Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 18 | |
| 19 | // Need to keep the log statements even in production builds |
| 20 | // to enable VERBOSE logging dynamically. |
| 21 | // You can enable VERBOSE logging as follows: |
| 22 | // adb shell setprop log.tag.APM_AudioPolicyManager V |
| 23 | #define LOG_NDEBUG 0 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 24 | |
| 25 | //#define VERY_VERBOSE_LOGGING |
| 26 | #ifdef VERY_VERBOSE_LOGGING |
| 27 | #define ALOGVV ALOGV |
| 28 | #else |
| 29 | #define ALOGVV(a...) do { } while(0) |
| 30 | #endif |
| 31 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 32 | #include <algorithm> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 33 | #include <inttypes.h> |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 34 | #include <map> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 35 | #include <math.h> |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 36 | #include <set> |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 37 | #include <type_traits> |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 38 | #include <unordered_set> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 39 | #include <vector> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 40 | |
| 41 | #include <Serializer.h> |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 42 | #include <android/media/audio/common/AudioPort.h> |
Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 43 | #include <cutils/bitops.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 44 | #include <cutils/properties.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 45 | #include <media/AudioParameter.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 46 | #include <policy.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 47 | #include <private/android_filesystem_config.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 48 | #include <system/audio.h> |
Mikhail Naganov | 27cf37c | 2020-04-14 14:47:01 -0700 | [diff] [blame] | 49 | #include <system/audio_config.h> |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 50 | #include <system/audio_effects/effect_hapticgenerator.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 51 | #include <utils/Log.h> |
| 52 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 53 | #include "AudioPolicyManager.h" |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 54 | #include "TypeConverter.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 55 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 56 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 57 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 58 | using android::media::audio::common::AudioDevice; |
| 59 | using android::media::audio::common::AudioDeviceAddress; |
| 60 | using android::media::audio::common::AudioPortDeviceExt; |
| 61 | using android::media::audio::common::AudioPortExt; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 62 | using content::AttributionSourceState; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 63 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 64 | //FIXME: workaround for truncated touch sounds |
| 65 | // to be removed when the problem is handled by system UI |
| 66 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 67 | |
| 68 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 69 | // media / notification / system volume. |
| 70 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 71 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 72 | template <typename T> |
| 73 | bool operator== (const SortedVector<T> &left, const SortedVector<T> &right) |
| 74 | { |
| 75 | if (left.size() != right.size()) { |
| 76 | return false; |
| 77 | } |
| 78 | for (size_t index = 0; index < right.size(); index++) { |
| 79 | if (left[index] != right[index]) { |
| 80 | return false; |
| 81 | } |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | template <typename T> |
| 87 | bool operator!= (const SortedVector<T> &left, const SortedVector<T> &right) |
| 88 | { |
| 89 | return !(left == right); |
| 90 | } |
| 91 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | // ---------------------------------------------------------------------------- |
| 93 | // AudioPolicyInterface implementation |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 96 | status_t AudioPolicyManager::setDeviceConnectionState(audio_policy_dev_state_t state, |
| 97 | const android::media::audio::common::AudioPort& port, audio_format_t encodedFormat) { |
| 98 | status_t status = setDeviceConnectionStateInt(state, port, encodedFormat); |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 99 | nextAudioPortGeneration(); |
| 100 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 103 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
| 104 | audio_policy_dev_state_t state, |
| 105 | const char* device_address, |
| 106 | const char* device_name, |
| 107 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 108 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 109 | if (status_t status = deviceToAudioPort(device, device_address, device_name, &aidlPort); |
| 110 | status == OK) { |
| 111 | return setDeviceConnectionState(state, aidlPort.hal, encodedFormat); |
| 112 | } else { |
| 113 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 114 | return status; |
| 115 | } |
| 116 | } |
| 117 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 118 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 119 | media::DeviceConnectedState state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 120 | { |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 121 | audio_port_v7 devicePort; |
| 122 | device->toAudioPort(&devicePort); |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 123 | if (status_t status = mpClientInterface->setDeviceConnectedState(&devicePort, state); |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 124 | status != OK) { |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 125 | ALOGE("Error %d while setting connected state for device %s", state, |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 126 | device->getDeviceTypeAddr().toString(false).c_str()); |
| 127 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 130 | status_t AudioPolicyManager::setDeviceConnectionStateInt( |
| 131 | audio_policy_dev_state_t state, const android::media::audio::common::AudioPort& port, |
| 132 | audio_format_t encodedFormat) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 133 | if (port.ext.getTag() != AudioPortExt::device) { |
| 134 | return BAD_VALUE; |
| 135 | } |
| 136 | audio_devices_t device_type; |
| 137 | std::string device_address; |
| 138 | if (status_t status = aidl2legacy_AudioDevice_audio_device( |
| 139 | port.ext.get<AudioPortExt::device>().device, &device_type, &device_address); |
| 140 | status != OK) { |
| 141 | return status; |
| 142 | }; |
| 143 | const char* device_name = port.name.c_str(); |
| 144 | // connect/disconnect only 1 device at a time |
| 145 | if (!audio_is_output_device(device_type) && !audio_is_input_device(device_type)) |
| 146 | return BAD_VALUE; |
| 147 | |
| 148 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 149 | device_type, device_address.c_str(), device_name, encodedFormat, |
| 150 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 151 | if (device == nullptr) { |
| 152 | return INVALID_OPERATION; |
| 153 | } |
| 154 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 155 | device->setExtraAudioDescriptors(port.extraAudioDescriptors); |
| 156 | } |
| 157 | return setDeviceConnectionStateInt(device, state); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 158 | } |
| 159 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 160 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 161 | audio_policy_dev_state_t state, |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 162 | const char* device_address, |
| 163 | const char* device_name, |
| 164 | audio_format_t encodedFormat) { |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 165 | media::AudioPortFw aidlPort; |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 166 | if (status_t status = deviceToAudioPort(deviceType, device_address, device_name, &aidlPort); |
| 167 | status == OK) { |
| 168 | return setDeviceConnectionStateInt(state, aidlPort.hal, encodedFormat); |
| 169 | } else { |
| 170 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 171 | return status; |
| 172 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 173 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 174 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 175 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 176 | audio_policy_dev_state_t state) |
| 177 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 178 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 179 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 180 | SortedVector <audio_io_handle_t> outputs; |
| 181 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 182 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 183 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | // save a copy of the opened output descriptors before any output is opened or closed |
| 185 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 186 | mPreviousOutputs = mOutputs; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 187 | |
| 188 | bool wasLeUnicastActive = isLeUnicastActive(); |
| 189 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 190 | switch (state) |
| 191 | { |
| 192 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 193 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 194 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 195 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 196 | return INVALID_OPERATION; |
| 197 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 198 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 199 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 202 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 203 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 204 | } |
| 205 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 206 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 207 | // parameters on newly connected devices (instead of opening the outputs...) |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 208 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 209 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 210 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 211 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 212 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 213 | mHwModules.cleanUpForDevice(device); |
| 214 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 215 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 216 | return INVALID_OPERATION; |
| 217 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 218 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 219 | // Populate encapsulation information when a output device is connected. |
| 220 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 221 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 222 | // outputs should never be empty here |
| 223 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 224 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 225 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 226 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 227 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 229 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 230 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 231 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 232 | return INVALID_OPERATION; |
| 233 | } |
| 234 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 235 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 236 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 237 | // Notify the HAL to prepare to disconnect device |
| 238 | broadcastDeviceConnectionState( |
| 239 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 240 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 241 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 242 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 243 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 244 | mOutputs.clearSessionRoutesForDevice(device); |
| 245 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 246 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 247 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 248 | // Send Disconnect to HALs |
| 249 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 250 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 251 | // Reset active device codec |
| 252 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 253 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 254 | // remove device from mReportedFormatsMap cache |
| 255 | mReportedFormatsMap.erase(device); |
| 256 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 257 | // remove preferred mixer configurations |
| 258 | mPreferredMixerAttrInfos.erase(device->getId()); |
| 259 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 260 | } break; |
| 261 | |
| 262 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 263 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 264 | return BAD_VALUE; |
| 265 | } |
| 266 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 267 | // Propagate device availability to Engine |
| 268 | setEngineDeviceConnectionState(device, state); |
| 269 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 270 | // No need to evaluate playback routing when connecting a remote submix |
| 271 | // output device used by a dynamic policy of type recorder as no |
| 272 | // playback use case is affected. |
| 273 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 274 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 275 | for (audio_io_handle_t output : outputs) { |
| 276 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 277 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 278 | if (policyMix != nullptr |
| 279 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 280 | && device->address() == policyMix->mDeviceAddress.string()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 281 | doCheckForDeviceAndOutputChanges = false; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 288 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 289 | if (!outputs.isEmpty()) { |
| 290 | for (audio_io_handle_t output : outputs) { |
| 291 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 292 | // close unused outputs after device disconnection or direct outputs that have |
| 293 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 294 | // "outputs" vector never contains duplicated outputs |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 295 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 296 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 297 | (desc->mDirectOpenCount == 0)) |
| 298 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) && |
| 299 | !isOutputOnlyAvailableRouteToSomeDevice(desc))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 300 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 301 | closeOutput(output); |
| 302 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 304 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 305 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 306 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 307 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | if (doCheckForDeviceAndOutputChanges) { |
| 311 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 312 | } else { |
| 313 | checkCloseOutputs(); |
| 314 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 315 | (void)updateCallRouting(false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 316 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 317 | const DeviceVector activeMediaDevices = |
| 318 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 319 | std::map<audio_io_handle_t, DeviceVector> outputsToReopenWithDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 320 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 321 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 322 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 323 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 324 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 325 | // do not force device change on duplicated output because if device is 0, it will |
| 326 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 327 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 328 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 329 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 330 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 331 | // always force when disconnecting (a non-duplicated device) |
| 332 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 333 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 334 | // If the device is using preferred mixer attributes, the output need to reopen |
| 335 | // with default configuration when the new selected devices are different from |
| 336 | // current routing devices |
| 337 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), newDevices); |
| 338 | continue; |
| 339 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 340 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 341 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 342 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 343 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 344 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 345 | // Reopen the output to query the dynamic profiles when there is not active |
| 346 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 347 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 348 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 349 | if (areAllActiveTracksRerouted(desc)) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 350 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), activeMediaDevices); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 351 | } else { |
| 352 | desc->mPendingReopenToQueryProfiles = true; |
| 353 | } |
| 354 | } |
| 355 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 356 | // Clear the flag that previously set for re-querying profiles. |
| 357 | desc->mPendingReopenToQueryProfiles = false; |
| 358 | } |
| 359 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 360 | reopenOutputsWithDevices(outputsToReopenWithDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 361 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 362 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 363 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 366 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, 0); |
| 367 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 368 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 369 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 370 | } // end if is output device |
| 371 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 372 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 373 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 374 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 375 | switch (state) |
| 376 | { |
| 377 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 378 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 379 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 380 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 381 | return INVALID_OPERATION; |
| 382 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 383 | |
| 384 | if (mAvailableInputDevices.add(device) < 0) { |
| 385 | return NO_MEMORY; |
| 386 | } |
| 387 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 388 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 389 | // parameters on newly connected devices (instead of opening the inputs...) |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 390 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::CONNECTED); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 391 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 392 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 393 | mAvailableInputDevices.remove(device); |
| 394 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 395 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 396 | |
| 397 | mHwModules.cleanUpForDevice(device); |
| 398 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 399 | return INVALID_OPERATION; |
| 400 | } |
| 401 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 402 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 403 | |
| 404 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 405 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 406 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 407 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 408 | return INVALID_OPERATION; |
| 409 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 410 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 411 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 412 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 413 | // Notify the HAL to prepare to disconnect device |
| 414 | broadcastDeviceConnectionState( |
| 415 | device, media::DeviceConnectedState::PREPARE_TO_DISCONNECT); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 416 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 417 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 418 | |
| 419 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 420 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 421 | // Set Disconnect to HALs |
| 422 | broadcastDeviceConnectionState(device, media::DeviceConnectedState::DISCONNECTED); |
| 423 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 424 | // remove device from mReportedFormatsMap cache |
| 425 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 426 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 427 | |
| 428 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 429 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 430 | return BAD_VALUE; |
| 431 | } |
| 432 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 433 | // Propagate device availability to Engine |
| 434 | setEngineDeviceConnectionState(device, state); |
| 435 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 436 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 437 | // As the input device list can impact the output device selection, update |
| 438 | // getDeviceForStrategy() cache |
| 439 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 440 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 441 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 442 | // Reconnect Audio Source |
| 443 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 444 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 445 | checkAudioSourceForAttributes(attributes); |
| 446 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 447 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 448 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 451 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 452 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 453 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 454 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 455 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 456 | return BAD_VALUE; |
| 457 | } |
| 458 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 459 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 460 | const char* device_name, |
Atneya Nair | 638a6e4 | 2022-12-18 16:45:15 -0800 | [diff] [blame] | 461 | media::AudioPortFw* aidlPort) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 462 | DeviceDescriptorBase devDescr(device, device_address); |
| 463 | devDescr.setName(device_name); |
| 464 | return devDescr.writeToParcelable(aidlPort); |
| 465 | } |
| 466 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 467 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 468 | audio_policy_dev_state_t state) { |
| 469 | |
| 470 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 471 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 472 | return; |
| 473 | } |
| 474 | mEngine->setDeviceConnectionState(device, state); |
| 475 | } |
| 476 | |
| 477 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 478 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 479 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 480 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 481 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 482 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 483 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 484 | (strlen(device_address) != 0)/*matchAddress*/); |
| 485 | |
| 486 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 487 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 488 | device, device_address); |
| 489 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 490 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 491 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 492 | DeviceVector *deviceVector; |
| 493 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 494 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 495 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 496 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 497 | deviceVector = &mAvailableInputDevices; |
| 498 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 499 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 500 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 501 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 502 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 503 | return (deviceVector->getDevice( |
| 504 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 505 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 508 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 509 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 510 | const char *device_name, |
| 511 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 512 | { |
| 513 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 514 | String8 reply; |
| 515 | AudioParameter param; |
| 516 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 517 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 518 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 519 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 520 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 521 | // connect/disconnect only 1 device at a time |
| 522 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 523 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 524 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 525 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 526 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 527 | // Nothing to do: device is not connected |
| 528 | return NO_ERROR; |
| 529 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 530 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 531 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 532 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 533 | // configure codecs. |
| 534 | // Handle two specific cases by sending a set parameter to |
| 535 | // configure A2DP codecs. No need to toggle device state. |
| 536 | // Case 1: A2DP active device switches from primary to primary |
| 537 | // module |
| 538 | // Case 2: A2DP device config changes on primary module. |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 539 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 540 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 541 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 542 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 543 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 544 | reply = mpClientInterface->getParameters( |
| 545 | AUDIO_IO_HANDLE_NONE, |
| 546 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 547 | AudioParameter repliedParameters(reply); |
| 548 | repliedParameters.getInt( |
| 549 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 550 | if (isReconfigA2dpSupported) { |
| 551 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 552 | param.add(key, String8("true")); |
| 553 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 554 | devDesc->setEncodedFormat(encodedFormat); |
| 555 | return NO_ERROR; |
| 556 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 559 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 560 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 561 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 562 | // mute media strategies and delay device switch by the largest |
| 563 | // This avoid sending the music tail into the earpiece or headset. |
| 564 | setStrategyMute(musicStrategy, true, desc); |
| 565 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 566 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 567 | nullptr, true /*fromCache*/).types()); |
| 568 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 569 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 570 | // This will force reading again the device configuration |
| 571 | status = setDeviceConnectionState(device, |
| 572 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 573 | device_address, device_name, |
| 574 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 575 | if (status != NO_ERROR) { |
| 576 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 577 | status); |
| 578 | return status; |
| 579 | } |
| 580 | |
| 581 | status = setDeviceConnectionState(device, |
| 582 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 583 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 584 | if (status != NO_ERROR) { |
| 585 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 586 | status); |
| 587 | return status; |
| 588 | } |
| 589 | |
| 590 | return NO_ERROR; |
| 591 | } |
| 592 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 593 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 594 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 595 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 596 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 597 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 598 | std::unordered_set<audio_format_t> formatSet; |
| 599 | sp<HwModule> primaryModule = |
| 600 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 601 | if (primaryModule == nullptr) { |
| 602 | ALOGE("%s() unable to get primary module", __func__); |
| 603 | return NO_INIT; |
| 604 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 605 | |
| 606 | DeviceTypeSet audioDeviceSet; |
| 607 | |
| 608 | switch(device) { |
| 609 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 610 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 611 | break; |
| 612 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 613 | audioDeviceSet = getAudioDeviceOutLeAudioUnicastSet(); |
| 614 | break; |
| 615 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 616 | audioDeviceSet = getAudioDeviceOutLeAudioBroadcastSet(); |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 617 | break; |
| 618 | default: |
| 619 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 620 | return BAD_VALUE; |
| 621 | } |
| 622 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 623 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 624 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 625 | for (const auto& device : declaredDevices) { |
| 626 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 627 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 628 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 629 | return status; |
| 630 | } |
| 631 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 632 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 633 | { |
| 634 | DeviceVector rxSinkdevices{}; |
| 635 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 636 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 637 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 638 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 639 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 640 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 641 | // retrieve Rx Source device descriptor |
| 642 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 643 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 644 | |
| 645 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 646 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 647 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 648 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 649 | return DeviceVector(rxSinkDevice); |
| 650 | } |
| 651 | } |
| 652 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 653 | // the device returned is not necessarily reachable via this output |
| 654 | // (filter later by setOutputDevices()) |
| 655 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 656 | } |
| 657 | |
| 658 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 659 | { |
| 660 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
| 661 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 662 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 663 | } |
| 664 | return INVALID_OPERATION; |
| 665 | } |
| 666 | |
| 667 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 668 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 669 | { |
| 670 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 671 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 672 | uint32_t muteWaitMs = 0; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 673 | if(!hasPrimaryOutput() || |
| 674 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 675 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 676 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 677 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 678 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 679 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 680 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 681 | if (txSourceDevice == nullptr) { |
| 682 | ALOGE("%s() selected input device not available", __func__); |
| 683 | return INVALID_OPERATION; |
| 684 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 685 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 686 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 687 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 688 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 689 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 690 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 691 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 692 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 693 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 694 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 695 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 696 | // retrieve Rx Source and Tx Sink device descriptors |
| 697 | sp<DeviceDescriptor> rxSourceDevice = |
| 698 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 699 | String8(), |
| 700 | AUDIO_FORMAT_DEFAULT); |
| 701 | sp<DeviceDescriptor> txSinkDevice = |
| 702 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 703 | String8(), |
| 704 | AUDIO_FORMAT_DEFAULT); |
| 705 | |
| 706 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 707 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 708 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 709 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 710 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 711 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 712 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 713 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 714 | // createAudioPatchInternal now supports both HW / SW bridging |
| 715 | createRxPatch = true; |
| 716 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 717 | } else { |
| 718 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 719 | // voice calls via setOutputDevice() on primary output. |
| 720 | // Otherwise, create two audio patches for TX and RX path. |
| 721 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 722 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 723 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 724 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 725 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 726 | (txSinkDevice != 0); |
| 727 | } |
| 728 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 729 | // Otherwise, create two audio patches for TX and RX path. |
| 730 | if (!createRxPatch) { |
| 731 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 732 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 733 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 734 | // If the TX device is on the primary HW module but RX device is |
| 735 | // on other HW module, SinkMetaData of telephony input should handle it |
| 736 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 737 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 738 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 739 | // terminate active capture if on the same HW module as the call TX source device |
| 740 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 741 | // call TX device but this information is not in the audio patch and logic here must be |
| 742 | // symmetric to the one in startInput() |
| 743 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 744 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 745 | closeActiveClients(activeDesc); |
| 746 | } |
| 747 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 748 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 749 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 750 | if (waitMs != nullptr) { |
| 751 | *waitMs = muteWaitMs; |
| 752 | } |
| 753 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 754 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 755 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 756 | bool AudioPolicyManager::isDeviceOfModule( |
| 757 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 758 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 759 | if (module != 0) { |
| 760 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 761 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 762 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 763 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 764 | } |
| 765 | return false; |
| 766 | } |
| 767 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 768 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 769 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 770 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 771 | const struct audio_port_config source = { |
| 772 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 773 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 774 | }; |
| 775 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 776 | mCallRxSourceClient = startAudioSourceInternal(&source, &aa, 0/*uid*/); |
| 777 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 778 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 779 | } |
| 780 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 781 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 782 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 783 | if (clientDesc == nullptr) { |
| 784 | return; |
| 785 | } |
| 786 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 787 | "%s error stopping audio source", __func__); |
| 788 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 792 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 793 | uint32_t delayMs) |
| 794 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 795 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 796 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 797 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 798 | return; |
| 799 | } |
| 800 | PatchBuilder patchBuilder; |
| 801 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
| 802 | ALOGV("%s between source %s and sink %s", __func__, |
| 803 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 804 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 805 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 806 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 807 | struct audio_port_config source = {}; |
| 808 | srcDevice->toAudioPortConfig(&source); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 809 | mCallTxSourceClient = new InternalSourceClientDescriptor( |
| 810 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, sinkDevice, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 811 | mCommunnicationStrategy, toVolumeSource(aa)); |
| 812 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 813 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 814 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 815 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 816 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
| 817 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 818 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 819 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 822 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 823 | { |
| 824 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 825 | // store previous phone state for management of sonification strategy below |
| 826 | int oldState = mEngine->getPhoneState(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 827 | bool wasLeUnicastActive = isLeUnicastActive(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 828 | |
| 829 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 830 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 831 | return; |
| 832 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 833 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 834 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 835 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 836 | // force reevaluating accessibility routing when call stops |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 837 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 838 | } |
| 839 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 840 | /** |
| 841 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 842 | * routing command. |
| 843 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 844 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 845 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 846 | |
| 847 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 848 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 849 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 850 | int delayMs = 0; |
| 851 | if (isStateInCall(state)) { |
| 852 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 853 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 854 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 855 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 856 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 857 | // mute media and sonification strategies and delay device switch by the largest |
| 858 | // latency of any output where either strategy is active. |
| 859 | // This avoid sending the ring tone or music tail into the earpiece or headset. |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 860 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 861 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 862 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 863 | (delayMs < (int)desc->latency()*2)) { |
| 864 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 865 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 866 | setStrategyMute(musicStrategy, true, desc); |
| 867 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 868 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 869 | nullptr, true /*fromCache*/).types()); |
| 870 | setStrategyMute(sonificationStrategy, true, desc); |
| 871 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 872 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 873 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 877 | if (hasPrimaryOutput()) { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 878 | if (state == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 879 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 880 | } else { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 881 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 882 | // force routing command to audio hardware when ending call |
| 883 | // even if no device change is needed |
| 884 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 885 | rxDevices = mPrimaryOutput->devices(); |
| 886 | } |
| 887 | if (oldState == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 888 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 889 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 890 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 891 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 892 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 893 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 894 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 895 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 896 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 897 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 898 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 899 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 900 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 901 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 902 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 903 | // If the device is using preferred mixer attributes, the output need to reopen |
| 904 | // with default configuration when the new selected devices are different from |
| 905 | // current routing devices. |
| 906 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 907 | continue; |
| 908 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 909 | setOutputDevices(desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
| 910 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 913 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 914 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 915 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
| 916 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 917 | if (isStateInCall(state)) { |
| 918 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 919 | // force reevaluating accessibility routing when call starts |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 920 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 924 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 925 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 928 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 929 | return mEngine->getPhoneState(); |
| 930 | } |
| 931 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 932 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 933 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 934 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 935 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 936 | if (config == mEngine->getForceUse(usage)) { |
| 937 | return; |
| 938 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 939 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 940 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 941 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 942 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 944 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 945 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 946 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 947 | |
| 948 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 949 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 950 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 951 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 952 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 953 | invalidateStreams({AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE}); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 956 | //FIXME: workaround for truncated touch sounds |
| 957 | // to be removed when the problem is handled by system UI |
| 958 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 959 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 960 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 961 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 962 | |
| 963 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 964 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 967 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 968 | { |
| 969 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 970 | } |
| 971 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 972 | // Find an MSD output profile compatible with the parameters passed. |
| 973 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 974 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 975 | const DeviceVector& devices, |
| 976 | uint32_t samplingRate, |
| 977 | audio_format_t format, |
| 978 | audio_channel_mask_t channelMask, |
| 979 | audio_output_flags_t flags, |
| 980 | bool directOnly) |
| 981 | { |
| 982 | flags = getRelevantFlags(flags, directOnly); |
| 983 | |
| 984 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 985 | if (msdModule != nullptr) { |
| 986 | // for the msd module check if there are patches to the output devices |
| 987 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 988 | HwModuleCollection modules; |
| 989 | modules.add(msdModule); |
| 990 | return searchCompatibleProfileHwModules( |
| 991 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 992 | flags, directOnly); |
| 993 | } |
| 994 | } |
| 995 | return nullptr; |
| 996 | } |
| 997 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 998 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 999 | // search to profiles for direct outputs. |
| 1000 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1001 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1002 | uint32_t samplingRate, |
| 1003 | audio_format_t format, |
| 1004 | audio_channel_mask_t channelMask, |
| 1005 | audio_output_flags_t flags, |
| 1006 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1007 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1008 | flags = getRelevantFlags(flags, directOnly); |
| 1009 | |
| 1010 | return searchCompatibleProfileHwModules( |
| 1011 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 1012 | } |
| 1013 | |
| 1014 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 1015 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1016 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1017 | // only retain flags that will drive the direct output profile selection |
| 1018 | // if explicitly requested |
| 1019 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1020 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1021 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1022 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1023 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1024 | return flags; |
| 1025 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1026 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1027 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 1028 | const HwModuleCollection& hwModules, |
| 1029 | const DeviceVector& devices, |
| 1030 | uint32_t samplingRate, |
| 1031 | audio_format_t format, |
| 1032 | audio_channel_mask_t channelMask, |
| 1033 | audio_output_flags_t flags, |
| 1034 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1035 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1036 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1037 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1038 | if (!curProfile->isCompatibleProfile(devices, |
| 1039 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1040 | format, NULL /*updatedFormat*/, |
| 1041 | channelMask, NULL /*updatedChannelMask*/, |
| 1042 | flags)) { |
| 1043 | continue; |
| 1044 | } |
| 1045 | // reject profiles not corresponding to a device currently available |
| 1046 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1047 | continue; |
| 1048 | } |
| 1049 | // reject profiles if connected device does not support codec |
| 1050 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1051 | continue; |
| 1052 | } |
| 1053 | if (!directOnly) { |
| 1054 | return curProfile; |
| 1055 | } |
| 1056 | |
| 1057 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1058 | // to one with offload capability |
Patty Huang | 36028df | 2022-07-06 00:14:12 +0800 | [diff] [blame] | 1059 | if (profile != 0 && |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1060 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1061 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1062 | } |
| 1063 | profile = curProfile; |
| 1064 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1065 | break; |
| 1066 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1069 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1072 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1073 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1074 | { |
| 1075 | for (const auto& hwModule : mHwModules) { |
| 1076 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1077 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1078 | continue; |
| 1079 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1080 | if (!devices.empty()) { |
Eric Laurent | 0c8f7cc | 2022-06-24 14:32:36 +0200 | [diff] [blame] | 1081 | // reject profiles not corresponding to a device currently available |
| 1082 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1083 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1084 | continue; |
| 1085 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1086 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1087 | != devices.size()) { |
| 1088 | continue; |
| 1089 | } |
| 1090 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1091 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1092 | return curProfile; |
| 1093 | } |
| 1094 | } |
| 1095 | return nullptr; |
| 1096 | } |
| 1097 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1098 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1099 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1100 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1101 | |
| 1102 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1103 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1104 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1105 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1106 | // and AudioSystem::getOutputSamplingRate(). |
| 1107 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1108 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1109 | const audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1110 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1111 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1112 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1113 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1116 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1117 | const audio_attributes_t *srcAttr, |
| 1118 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1119 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1120 | if (srcAttr != NULL) { |
| 1121 | if (!isValidAttributes(srcAttr)) { |
| 1122 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1123 | __func__, |
| 1124 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1125 | srcAttr->tags); |
| 1126 | return BAD_VALUE; |
| 1127 | } |
| 1128 | *dstAttr = *srcAttr; |
| 1129 | } else { |
| 1130 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1131 | ALOGE("%s: invalid stream type", __func__); |
| 1132 | return BAD_VALUE; |
| 1133 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1134 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1135 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1136 | |
| 1137 | // Only honor audibility enforced when required. The client will be |
| 1138 | // forced to reconnect if the forced usage changes. |
| 1139 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1140 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1141 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1144 | return NO_ERROR; |
| 1145 | } |
| 1146 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1147 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1148 | audio_attributes_t *resultAttr, |
| 1149 | audio_io_handle_t *output, |
| 1150 | audio_session_t session, |
| 1151 | const audio_attributes_t *attr, |
| 1152 | audio_stream_type_t *stream, |
| 1153 | uid_t uid, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1154 | audio_config_t *config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1155 | audio_output_flags_t *flags, |
| 1156 | audio_port_handle_t *selectedDeviceId, |
| 1157 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1158 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1159 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1160 | bool *isSpatialized, |
| 1161 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1162 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1163 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1164 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1165 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1166 | const sp<DeviceDescriptor> requestedDevice = |
| 1167 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1168 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1169 | *outputType = API_OUTPUT_INVALID; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1170 | *isSpatialized = false; |
| 1171 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1172 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1173 | if (status != NO_ERROR) { |
| 1174 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1175 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1176 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1177 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1178 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1179 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1180 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1181 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1182 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1183 | |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1184 | bool usePrimaryOutputFromPolicyMixes = false; |
| 1185 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1186 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1187 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1188 | // Secondary outputs are always found by dynamic policies as the engine do not support them |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1189 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1190 | const audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1191 | .channel_mask = config->channel_mask, |
| 1192 | .format = config->format, |
| 1193 | }; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1194 | status = mPolicyMixes.getOutputForAttr(*resultAttr, clientConfig, uid, session, *flags, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 1195 | mAvailableOutputDevices, requestedDevice, primaryMix, |
| 1196 | secondaryMixes, usePrimaryOutputFromPolicyMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1197 | if (status != OK) { |
| 1198 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1199 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1200 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1201 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1202 | if ((secondaryMixes != nullptr && !secondaryMixes->empty()) |
| 1203 | && !audio_is_linear_pcm(config->format)) { |
| 1204 | ALOGD("%s: rejecting request as secondary mixes only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1205 | return BAD_VALUE; |
| 1206 | } |
| 1207 | if (usePrimaryOutputFromPolicyMixes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1208 | sp<DeviceDescriptor> deviceDesc = |
| 1209 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1210 | primaryMix->mDeviceAddress, |
| 1211 | AUDIO_FORMAT_DEFAULT); |
| 1212 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1213 | bool tryDirectForFlags = policyDesc == nullptr || |
| 1214 | (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT); |
| 1215 | // if a direct output can be opened to deliver the track's multi-channel content to the |
| 1216 | // output rather than being downmixed by the primary output, then use this direct |
| 1217 | // output by by-passing the primary mix if possible, otherwise fall-through to primary |
| 1218 | // mix. |
| 1219 | bool tryDirectForChannelMask = policyDesc != nullptr |
| 1220 | && (audio_channel_count_from_out_mask(policyDesc->getConfig().channel_mask) < |
| 1221 | audio_channel_count_from_out_mask(config->channel_mask)); |
| 1222 | if (deviceDesc != nullptr && (tryDirectForFlags || tryDirectForChannelMask)) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1223 | audio_io_handle_t newOutput; |
| 1224 | status = openDirectOutput( |
| 1225 | *stream, session, config, |
| 1226 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
| 1227 | DeviceVector(deviceDesc), &newOutput); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1228 | if (status == NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1229 | policyDesc = mOutputs.valueFor(newOutput); |
| 1230 | primaryMix->setOutput(policyDesc); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1231 | } else if (tryDirectForFlags) { |
| 1232 | policyDesc = nullptr; |
| 1233 | } // otherwise use primary if available. |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1234 | } |
| 1235 | if (policyDesc != nullptr) { |
| 1236 | policyDesc->mPolicyMix = primaryMix; |
| 1237 | *output = policyDesc->mIoHandle; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1238 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1239 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1240 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1241 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1242 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1243 | } else { |
| 1244 | *outputType = API_OUTPUT_LEGACY; |
| 1245 | } |
| 1246 | return NO_ERROR; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1247 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1248 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1249 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1250 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1251 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1252 | return BAD_VALUE; |
| 1253 | } |
| 1254 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1255 | // in order to let the choice of the order to future vendor engine |
| 1256 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1257 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1258 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1259 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1262 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1263 | // chosen by the engine if not. |
| 1264 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1265 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1266 | // TODO: Remove check of AUDIO_STREAM_MUSIC once migration is completed on the app side. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1267 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1268 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1269 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1270 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1271 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1272 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1273 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1277 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1278 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1279 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1280 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1281 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1282 | if (!msdDevices.isEmpty()) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1283 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags, isSpatialized); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1284 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1285 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1286 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1287 | } else { |
| 1288 | *output = AUDIO_IO_HANDLE_NONE; |
| 1289 | } |
| 1290 | } |
| 1291 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1292 | sp<PreferredMixerAttributesInfo> info = nullptr; |
| 1293 | if (outputDevices.size() == 1) { |
| 1294 | info = getPreferredMixerAttributesInfo( |
| 1295 | outputDevices.itemAt(0)->getId(), |
| 1296 | mEngine->getProductStrategyForAttributes(*resultAttr)); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1297 | // Only use preferred mixer if the uid matches or the preferred mixer is bit-perfect |
| 1298 | // and it is currently active. |
| 1299 | if (info != nullptr && info->getUid() != uid && |
| 1300 | ((info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) == AUDIO_OUTPUT_FLAG_NONE || |
| 1301 | info->getActiveClientCount() == 0)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1302 | info = nullptr; |
| 1303 | } |
| 1304 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1305 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1306 | flags, isSpatialized, info, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1307 | // The client will be active if the client is currently preferred mixer owner and the |
| 1308 | // requested configuration matches the preferred mixer configuration. |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1309 | *isBitPerfect = (info != nullptr |
| 1310 | && (info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE |
jiabin | 5eaf096 | 2022-12-20 20:11:38 +0000 | [diff] [blame] | 1311 | && info->getUid() == uid |
| 1312 | && *output != AUDIO_IO_HANDLE_NONE |
| 1313 | // When bit-perfect output is selected for the preferred mixer attributes owner, |
| 1314 | // only need to consider the config matches. |
| 1315 | && mOutputs.valueFor(*output)->isConfigurationMatched( |
| 1316 | clientConfig, AUDIO_OUTPUT_FLAG_NONE)); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1317 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1318 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1319 | AudioProfileVector profiles; |
| 1320 | status_t ret = getProfilesForDevices(outputDevices, profiles, *flags, false /*isInput*/); |
| 1321 | if (ret == NO_ERROR && !profiles.empty()) { |
| 1322 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 1323 | : *profiles[0]->getChannels().begin(); |
| 1324 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 1325 | : *profiles[0]->getSampleRates().begin(); |
| 1326 | config->format = profiles[0]->getFormat(); |
| 1327 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1328 | return INVALID_OPERATION; |
| 1329 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1330 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1331 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1332 | for (auto &outputDevice : outputDevices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 1333 | if (outputDevice->getId() == mConfig->getDefaultOutputDevice()->getId()) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1334 | *selectedDeviceId = outputDevice->getId(); |
| 1335 | break; |
| 1336 | } |
| 1337 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1338 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1339 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1340 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1341 | } else { |
| 1342 | *outputType = API_OUTPUT_LEGACY; |
| 1343 | } |
| 1344 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1345 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1346 | |
| 1347 | return NO_ERROR; |
| 1348 | } |
| 1349 | |
| 1350 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1351 | audio_io_handle_t *output, |
| 1352 | audio_session_t session, |
| 1353 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1354 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1355 | audio_config_t *config, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1356 | audio_output_flags_t *flags, |
| 1357 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1358 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1359 | std::vector<audio_io_handle_t> *secondaryOutputs, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1360 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1361 | bool *isSpatialized, |
| 1362 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1363 | { |
| 1364 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1365 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1366 | return INVALID_OPERATION; |
| 1367 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1368 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1369 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1370 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1371 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1372 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1373 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1374 | const sp<DeviceDescriptor> requestedDevice = |
| 1375 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1376 | |
| 1377 | // Prevent from storing invalid requested device id in clients |
| 1378 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1379 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1380 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1381 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1382 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1383 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1384 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType, isSpatialized, |
| 1385 | isBitPerfect); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1386 | if (status != NO_ERROR) { |
| 1387 | return status; |
| 1388 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1389 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1390 | if (secondaryOutputs != nullptr) { |
| 1391 | for (auto &secondaryMix : secondaryMixes) { |
| 1392 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1393 | if (outputDesc != nullptr && |
| 1394 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1395 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1396 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1397 | } |
| 1398 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1399 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1400 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1401 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1402 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1403 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1404 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1405 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1406 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1407 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1408 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1409 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1410 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1411 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1412 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1413 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1414 | std::move(weakSecondaryOutputDescs), |
| 1415 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1416 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1417 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1418 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1419 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1420 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1421 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1424 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1425 | audio_session_t session, |
| 1426 | const audio_config_t *config, |
| 1427 | audio_output_flags_t flags, |
| 1428 | const DeviceVector &devices, |
| 1429 | audio_io_handle_t *output) { |
| 1430 | |
| 1431 | *output = AUDIO_IO_HANDLE_NONE; |
| 1432 | |
| 1433 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1434 | // and not explicitly requested |
| 1435 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1436 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1437 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1438 | return NAME_NOT_FOUND; |
| 1439 | } |
| 1440 | |
| 1441 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1442 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1443 | // when audioflinger detects there is an active non offloadable effect. |
| 1444 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1445 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1446 | // in the background. |
| 1447 | sp<IOProfile> profile; |
| 1448 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1449 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1450 | profile = getProfileForOutput( |
| 1451 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1452 | flags, true /* directOnly */); |
| 1453 | } |
| 1454 | |
| 1455 | if (profile == nullptr) { |
| 1456 | return NAME_NOT_FOUND; |
| 1457 | } |
| 1458 | |
| 1459 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1460 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1461 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1462 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1463 | // reuse direct output if currently open by the same client |
| 1464 | // and configured with same parameters |
| 1465 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1466 | (config->format == desc->getFormat()) && |
| 1467 | (config->channel_mask == desc->getChannelMask()) && |
| 1468 | (session == desc->mDirectClientSession)) { |
| 1469 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1470 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1471 | mOutputs.keyAt(i), session); |
| 1472 | *output = mOutputs.keyAt(i); |
| 1473 | return NO_ERROR; |
| 1474 | } |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | if (!profile->canOpenNewIo()) { |
| 1479 | return NAME_NOT_FOUND; |
| 1480 | } |
| 1481 | |
| 1482 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1483 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 1484 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1485 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1486 | // 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] | 1487 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1488 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1489 | status_t status = |
| 1490 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, flags, output); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1491 | |
| 1492 | // only accept an output with the requested parameters |
| 1493 | if (status != NO_ERROR || |
| 1494 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1495 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1496 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1497 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1498 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1499 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1500 | config->channel_mask, outputDesc->getChannelMask()); |
| 1501 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1502 | outputDesc->close(); |
| 1503 | } |
| 1504 | // fall back to mixer output if possible when the direct output could not be open |
| 1505 | if (audio_is_linear_pcm(config->format) && |
| 1506 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1507 | return NAME_NOT_FOUND; |
| 1508 | } |
| 1509 | *output = AUDIO_IO_HANDLE_NONE; |
| 1510 | return BAD_VALUE; |
| 1511 | } |
| 1512 | outputDesc->mDirectOpenCount = 1; |
| 1513 | outputDesc->mDirectClientSession = session; |
| 1514 | |
| 1515 | addOutput(*output, outputDesc); |
| 1516 | mPreviousOutputs = mOutputs; |
| 1517 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1518 | mpClientInterface->onAudioPortListUpdate(); |
| 1519 | return NO_ERROR; |
| 1520 | } |
| 1521 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1522 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1523 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1524 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1525 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1526 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1527 | audio_output_flags_t *flags, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1528 | bool *isSpatialized, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1529 | sp<PreferredMixerAttributesInfo> prefMixerConfigInfo, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1530 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1531 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1532 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1533 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1534 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1535 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1536 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1537 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1538 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1539 | // open a direct output if required by specified parameters |
| 1540 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1541 | // and all common behaviors are driven by checking only the direct flag |
| 1542 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1543 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1544 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1545 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1546 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1547 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1548 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1549 | |
| 1550 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1551 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1552 | // only allow deep buffering for music stream type |
| 1553 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1554 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1555 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1556 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1557 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1558 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1559 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1560 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1561 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1562 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1563 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1564 | audio_is_linear_pcm(config->format) && |
| 1565 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1566 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1567 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1568 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1569 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1570 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1571 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1572 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1573 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1574 | } |
| 1575 | |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1576 | *isSpatialized = false; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1577 | if (mSpatializerOutput != nullptr |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 1578 | && canBeSpatializedInt(attr, config, devices.toTypeAddrVector())) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1579 | *isSpatialized = true; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1580 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1581 | } |
| 1582 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1583 | audio_config_t directConfig = *config; |
| 1584 | directConfig.channel_mask = channelMask; |
| 1585 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1586 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1587 | return output; |
| 1588 | } |
| 1589 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1590 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1591 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1592 | 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] | 1593 | return AUDIO_IO_HANDLE_NONE; |
| 1594 | } |
Pierre Couillaud | e73496b | 2023-03-06 16:19:05 +0000 | [diff] [blame] | 1595 | // A request for Tuner cannot fallback to a mixed output |
| 1596 | if ((directConfig.offload_info.content_id || directConfig.offload_info.sync_id)) { |
| 1597 | return AUDIO_IO_HANDLE_NONE; |
| 1598 | } |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1599 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1600 | // ignoring channel mask due to downmix capability in mixer |
| 1601 | |
| 1602 | // open a non direct output |
| 1603 | |
| 1604 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1605 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1606 | // get which output is suitable for the specified stream. The actual |
| 1607 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1608 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1609 | if (prefMixerConfigInfo != nullptr) { |
| 1610 | for (audio_io_handle_t outputHandle : outputs) { |
| 1611 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputHandle); |
| 1612 | if (outputDesc->mProfile == prefMixerConfigInfo->getProfile()) { |
| 1613 | output = outputHandle; |
| 1614 | break; |
| 1615 | } |
| 1616 | } |
| 1617 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1618 | // No output open with the preferred profile. Open a new one. |
| 1619 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1620 | config.channel_mask = prefMixerConfigInfo->getConfigBase().channel_mask; |
| 1621 | config.sample_rate = prefMixerConfigInfo->getConfigBase().sample_rate; |
| 1622 | config.format = prefMixerConfigInfo->getConfigBase().format; |
| 1623 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 1624 | prefMixerConfigInfo->getProfile(), devices, nullptr /*mixerConfig*/, |
| 1625 | &config, prefMixerConfigInfo->getFlags()); |
| 1626 | if (preferredOutput == nullptr) { |
| 1627 | ALOGE("%s failed to open output with preferred mixer config", __func__); |
| 1628 | } else { |
| 1629 | output = preferredOutput->mIoHandle; |
| 1630 | } |
| 1631 | } |
| 1632 | } else { |
| 1633 | // at this stage we should ignore the DIRECT flag as no direct output could be |
| 1634 | // found earlier |
| 1635 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1636 | output = selectOutput( |
| 1637 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
| 1638 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1639 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1640 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1641 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1642 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1643 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1644 | return output; |
| 1645 | } |
| 1646 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1647 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1648 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1649 | mAvailableInputDevices); |
| 1650 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1651 | } |
| 1652 | |
| 1653 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1654 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1655 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1656 | } |
| 1657 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1658 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1659 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1660 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1661 | if (msdModule != 0) { |
| 1662 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1663 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1664 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1665 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1666 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1667 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1668 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1669 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | return msdPatches; |
| 1674 | } |
| 1675 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1676 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1677 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1678 | if (index < 0) { |
| 1679 | return false; |
| 1680 | } |
| 1681 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1682 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1683 | if (msdModule == nullptr) { |
| 1684 | return false; |
| 1685 | } |
| 1686 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1687 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1688 | return true; |
| 1689 | } |
| 1690 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1691 | if (index < 0) { |
| 1692 | return false; |
| 1693 | } |
| 1694 | return true; |
| 1695 | } |
| 1696 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1697 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1698 | const InputProfileCollection &inputProfiles, |
| 1699 | const OutputProfileCollection &outputProfiles, |
| 1700 | const sp<DeviceDescriptor> &sourceDevice, |
| 1701 | const sp<DeviceDescriptor> &sinkDevice, |
| 1702 | AudioProfileVector& sourceProfiles, |
| 1703 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1704 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1705 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1706 | return NO_INIT; |
| 1707 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1708 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1709 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1710 | return NO_INIT; |
| 1711 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1712 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1713 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1714 | inProfile->supportsDevice(sourceDevice)) { |
| 1715 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1716 | } |
| 1717 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1718 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1719 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1720 | outProfile->supportsDevice(sinkDevice)) { |
| 1721 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1722 | } |
| 1723 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1724 | return NO_ERROR; |
| 1725 | } |
| 1726 | |
| 1727 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1728 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1729 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1730 | { |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1731 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 1732 | static const std::vector<audio_format_t> formatsOrder = {{ |
| 1733 | AUDIO_FORMAT_IEC60958, AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 1734 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 1735 | static const std::vector<audio_channel_mask_t> channelMasksOrder = [](){ |
| 1736 | // Channel position masks for MSD module, 3D > 2D > 1D ordering (most preferred to least |
| 1737 | // preferred). |
| 1738 | std::vector<audio_channel_mask_t> masks = {{ |
| 1739 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 1740 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 1741 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 1742 | // insert index masks (higher counts most preferred) as preferred over position masks |
| 1743 | for (int i = 1; i <= AUDIO_CHANNEL_COUNT_MAX; i++) { |
| 1744 | masks.insert( |
| 1745 | masks.begin(), audio_channel_mask_for_index_assignment_from_count(i)); |
| 1746 | } |
| 1747 | return masks; |
| 1748 | }(); |
| 1749 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1750 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1751 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, formatsOrder, |
| 1752 | channelMasksOrder, true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1753 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1754 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1755 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1756 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1757 | } |
| 1758 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1759 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1760 | sinkConfig->format = bestSinkConfig.format; |
| 1761 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1762 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1763 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1764 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1765 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1766 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1767 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1768 | // raw and IEC61937 framed streams. |
| 1769 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1770 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1771 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1772 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1773 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
Dean Wheatley | 16809da | 2022-12-09 14:55:46 +1100 | [diff] [blame] | 1774 | sourceConfig->channel_mask = |
| 1775 | audio_channel_mask_get_representation(bestSinkConfig.channel_mask) |
| 1776 | == AUDIO_CHANNEL_REPRESENTATION_INDEX ? |
| 1777 | bestSinkConfig.channel_mask : audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1778 | sourceConfig->format = bestSinkConfig.format; |
| 1779 | // Copy input stream directly without any processing (e.g. resampling). |
| 1780 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1781 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1782 | if (hwAvSync) { |
| 1783 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1784 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1785 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1786 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1787 | } |
| 1788 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1789 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1790 | sinkConfig->config_mask |= config_mask; |
| 1791 | sourceConfig->config_mask |= config_mask; |
| 1792 | return NO_ERROR; |
| 1793 | } |
| 1794 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1795 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 1796 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1797 | { |
| 1798 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1799 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1800 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 1801 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 1802 | if (deviceModule == nullptr) { |
| 1803 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 1804 | return patchBuilder; |
| 1805 | } |
| 1806 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 1807 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 1808 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 1809 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 1810 | |
| 1811 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 1812 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 1813 | device : getMsdAudioOutDevices().itemAt(0); |
| 1814 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 1815 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1816 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1817 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1818 | AudioProfileVector sourceProfiles; |
| 1819 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1820 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1821 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1822 | for (auto hwAvSync : { true, false }) { |
| 1823 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 1824 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 1825 | continue; |
| 1826 | } |
| 1827 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 1828 | &sinkConfig) == NO_ERROR) { |
| 1829 | // Found a matching config. Re-create PatchBuilder with this config. |
| 1830 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1831 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1832 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1833 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1834 | " supporting PCM format conversion.", __func__); |
| 1835 | return patchBuilder; |
| 1836 | } |
| 1837 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1838 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1839 | DeviceVector devices; |
| 1840 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1841 | devices.add(*outputDevices); |
| 1842 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1843 | // Use media strategy for unspecified output device. This should only |
| 1844 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1845 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1846 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1847 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1848 | 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] | 1849 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1850 | std::vector<PatchBuilder> patchesToCreate; |
| 1851 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1852 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1853 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1854 | } |
| 1855 | // Retain only the MSD patches associated with outputDevices request. |
| 1856 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1857 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1858 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1859 | auto retainedPatch = false; |
| 1860 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1861 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1862 | patchesToRemove.removeItemsAt(i); |
| 1863 | retainedPatch = true; |
| 1864 | break; |
| 1865 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1866 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1867 | if (retainedPatch) { |
| 1868 | it = patchesToCreate.erase(it); |
| 1869 | continue; |
| 1870 | } |
| 1871 | ++it; |
| 1872 | } |
| 1873 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1874 | return NO_ERROR; |
| 1875 | } |
| 1876 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1877 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1878 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1879 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1880 | status_t status = NO_ERROR; |
| 1881 | for (const auto &p : patchesToCreate) { |
| 1882 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1883 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1884 | char message[256]; |
| 1885 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1886 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1887 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1888 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1889 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1890 | if (currStatus == NO_ERROR) { |
| 1891 | ALOGD("%s", message); |
| 1892 | } else { |
| 1893 | ALOGE("%s", message); |
| 1894 | if (status == NO_ERROR) { |
| 1895 | status = currStatus; |
| 1896 | } |
| 1897 | } |
| 1898 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1899 | return status; |
| 1900 | } |
| 1901 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1902 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 1903 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1904 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1905 | const auto& patch = msdPatches[i]; |
| 1906 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1907 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1908 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1909 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1910 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1911 | break; |
| 1912 | } |
| 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1917 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 1918 | DeviceVector devicesToCheck = |
| 1919 | mConfig->getOutputDevices().getDevicesFromDeviceTypeAddrVec(devices); |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1920 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 1921 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1922 | const auto& patch = msdPatches[i]; |
| 1923 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1924 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1925 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1926 | const auto& foundDevice = devicesToCheck.getDevice( |
| 1927 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 1928 | if (foundDevice != nullptr) { |
| 1929 | devicesToCheck.remove(foundDevice); |
| 1930 | if (devicesToCheck.isEmpty()) { |
| 1931 | return true; |
| 1932 | } |
| 1933 | } |
| 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | return false; |
| 1938 | } |
| 1939 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1940 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1941 | audio_output_flags_t flags, |
| 1942 | audio_format_t format, |
| 1943 | audio_channel_mask_t channelMask, |
| 1944 | uint32_t samplingRate, |
| 1945 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1946 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1947 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 1948 | "%s called with format %#x", __func__, format); |
| 1949 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1950 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 1951 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 1952 | // haptic-generating effect attached to is in given outputs. |
| 1953 | if (sessionId != AUDIO_SESSION_NONE) { |
| 1954 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 1955 | sessionId, FX_IID_HAPTICGENERATOR); |
| 1956 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 1957 | return hapticGeneratingOutput; |
| 1958 | } |
| 1959 | } |
| 1960 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1961 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 1962 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 1963 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1964 | |
| 1965 | // Flags expressing a functional request: must be honored in priority over |
| 1966 | // other criteria |
| 1967 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 1968 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 1969 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 1970 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1971 | // Flags expressing a performance request: have lower priority than serving |
| 1972 | // requested sampling rate or channel mask |
| 1973 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 1974 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 1975 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 1976 | |
| 1977 | const audio_output_flags_t functionalFlags = |
| 1978 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 1979 | const audio_output_flags_t performanceFlags = |
| 1980 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 1981 | |
| 1982 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 1983 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1984 | // 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] | 1985 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1986 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1987 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1988 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1989 | // with tiebreak preferring the minimum number of extra functional flags |
| 1990 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1991 | // 3: the output supporting the exact channel mask |
| 1992 | // 4: the output with a higher channel count than requested |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 1993 | // 5: the output with the highest sampling rate if the requested sample rate is |
| 1994 | // greater than default sampling rate |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1995 | // 6: the output with the highest number of requested performance flags |
| 1996 | // 7: the output with the bit depth the closest to the requested one |
| 1997 | // 8: the primary output |
| 1998 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1999 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2000 | // matching criteria values in priority order for best matching output so far |
| 2001 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2002 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2003 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 2004 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 2005 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 2006 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2007 | for (audio_io_handle_t output : outputs) { |
| 2008 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2009 | // matching criteria values in priority order for current output |
| 2010 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 2011 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2012 | if (outputDesc->isDuplicated()) { |
| 2013 | continue; |
| 2014 | } |
| 2015 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 2016 | continue; |
| 2017 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2018 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2019 | // If haptic channel is specified, use the haptic output if present. |
| 2020 | // When using haptic output, same audio format and sample rate are required. |
| 2021 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2022 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2023 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 2024 | continue; |
| 2025 | } |
| 2026 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2027 | && format == outputDesc->getFormat() |
| 2028 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2029 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 2030 | } |
| 2031 | |
| 2032 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 2033 | const int matchingFunctionalFlags = |
| 2034 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 2035 | const int totalFunctionalFlags = |
| 2036 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 2037 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 2038 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2039 | |
| 2040 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2041 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 2042 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2043 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 2044 | channelCount <= outputChannelCount) { |
| 2045 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2046 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 2047 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2048 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2049 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2050 | currentMatchCriteria[3] = outputChannelCount; |
| 2051 | } |
| 2052 | |
| 2053 | // sampling rate match |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2054 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2055 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | // performance flags match |
| 2059 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 2060 | |
| 2061 | // format match |
| 2062 | if (format != AUDIO_FORMAT_INVALID) { |
| 2063 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2064 | PolicyAudioPort::kFormatDistanceMax - |
| 2065 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | // primary output match |
| 2069 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 2070 | |
| 2071 | // compare match criteria by priority then value |
| 2072 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2073 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 2074 | bestMatchCriteria = currentMatchCriteria; |
| 2075 | bestOutput = output; |
| 2076 | |
| 2077 | std::stringstream result; |
| 2078 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2079 | std::ostream_iterator<int>(result, " ")); |
| 2080 | ALOGV("%s new bestOutput %d criteria %s", |
| 2081 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2082 | } |
| 2083 | } |
| 2084 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2085 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2086 | } |
| 2087 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2088 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2089 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2090 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2091 | |
| 2092 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2093 | if (outputDesc == 0) { |
| 2094 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2095 | return BAD_VALUE; |
| 2096 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2097 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2098 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2099 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2100 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2101 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2102 | status_t status = outputDesc->start(); |
| 2103 | if (status != NO_ERROR) { |
| 2104 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2105 | } |
| 2106 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2107 | uint32_t delayMs; |
| 2108 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2109 | |
| 2110 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2111 | outputDesc->stop(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2112 | if (status == DEAD_OBJECT) { |
| 2113 | sp<SwAudioOutputDescriptor> desc = |
| 2114 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2115 | if (desc == nullptr) { |
| 2116 | // This is not common, it may indicate something wrong with the HAL. |
| 2117 | ALOGE("%s unable to open output with default config", __func__); |
| 2118 | return status; |
| 2119 | } |
| 2120 | desc->mUsePreferredMixerAttributes = true; |
| 2121 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2122 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2123 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2124 | |
| 2125 | // If the client is the first one active on preferred mixer parameters, reopen the output |
| 2126 | // if the current mixer parameters doesn't match the preferred one. |
| 2127 | if (outputDesc->devices().size() == 1) { |
| 2128 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2129 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2130 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2131 | if (info->getActiveClientCount() == 0 && !outputDesc->isConfigurationMatched( |
| 2132 | info->getConfigBase(), info->getFlags())) { |
| 2133 | stopSource(outputDesc, client); |
| 2134 | outputDesc->stop(); |
| 2135 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2136 | config.channel_mask = info->getConfigBase().channel_mask; |
| 2137 | config.sample_rate = info->getConfigBase().sample_rate; |
| 2138 | config.format = info->getConfigBase().format; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2139 | sp<SwAudioOutputDescriptor> desc = |
| 2140 | reopenOutput(outputDesc, &config, info->getFlags(), __func__); |
| 2141 | if (desc == nullptr) { |
| 2142 | return BAD_VALUE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2143 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2144 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2145 | // Intentionally return error to let the client side resending request for |
| 2146 | // creating and starting. |
| 2147 | return DEAD_OBJECT; |
| 2148 | } |
| 2149 | info->increaseActiveClient(); |
| 2150 | } |
| 2151 | } |
| 2152 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2153 | if (client->hasPreferredDevice()) { |
| 2154 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2155 | mpClientInterface->onRoutingUpdated(); |
| 2156 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2157 | if (delayMs != 0) { |
| 2158 | usleep(delayMs * 1000); |
| 2159 | } |
| 2160 | |
| 2161 | return status; |
| 2162 | } |
| 2163 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2164 | bool AudioPolicyManager::isLeUnicastActive() const { |
| 2165 | if (isInCall()) { |
| 2166 | return true; |
| 2167 | } |
| 2168 | return isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet()); |
| 2169 | } |
| 2170 | |
| 2171 | bool AudioPolicyManager::isAnyDeviceTypeActive(const DeviceTypeSet& deviceTypes) const { |
| 2172 | if (mAvailableOutputDevices.getDevicesFromTypes(deviceTypes).isEmpty()) { |
| 2173 | return false; |
| 2174 | } |
| 2175 | bool active = mOutputs.isAnyDeviceTypeActive(deviceTypes); |
| 2176 | ALOGV("%s active %d", __func__, active); |
| 2177 | return active; |
| 2178 | } |
| 2179 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2180 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2181 | const sp<TrackClientDescriptor>& client, |
| 2182 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2183 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2184 | // cannot start playback of STREAM_TTS if any other output is being used |
| 2185 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2186 | |
| 2187 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2188 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2189 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2190 | auto clientStrategy = client->strategy(); |
| 2191 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2192 | if (stream == AUDIO_STREAM_TTS) { |
| 2193 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2194 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 2195 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2196 | return INVALID_OPERATION; |
| 2197 | } else { |
| 2198 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2199 | } |
| 2200 | } else { |
| 2201 | // some playback other than beacon starts |
| 2202 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2203 | } |
| 2204 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2205 | // 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] | 2206 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2207 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2208 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2209 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2210 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2211 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2212 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2213 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2214 | address = policyMix->mDeviceAddress.string(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2215 | 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] | 2216 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2217 | } else { |
| 2218 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2219 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2220 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2221 | AUDIO_FORMAT_DEFAULT); |
| 2222 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2223 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2224 | } |
| 2225 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2226 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2227 | // It covers a common case when there is no materially active audio |
| 2228 | // and muting would result in unnecessary delay and dropped audio. |
| 2229 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2230 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2231 | bool wasLeUnicastActive = isLeUnicastActive(); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2232 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2233 | // increment usage count for this stream on the requested output: |
| 2234 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2235 | // 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] | 2236 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2237 | |
| 2238 | if (client->hasPreferredDevice(true)) { |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2239 | if (outputDesc->sameExclusivePreferredDevicesCount() > 0) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2240 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2241 | devices = DeviceVector( |
| 2242 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2243 | } else { |
| 2244 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2245 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2246 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2247 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2248 | } |
| 2249 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2250 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2251 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2252 | selectOutputForMusicEffects(); |
| 2253 | } |
| 2254 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2255 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2256 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2257 | if (devices.isEmpty()) { |
| 2258 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2259 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2260 | bool shouldWait = |
| 2261 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2262 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2263 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2264 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2265 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2266 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2267 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2268 | // An output has a shared device if |
| 2269 | // - managed by the same hw module |
| 2270 | // - supports the currently selected device |
| 2271 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2272 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2273 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2274 | // force a device change if any other output is: |
| 2275 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2276 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2277 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2278 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2279 | // 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] | 2280 | // change the device currently selected by the other output. |
| 2281 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2282 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2283 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2284 | force = true; |
| 2285 | } |
| 2286 | // 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] | 2287 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2288 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2289 | const uint32_t latencyMs = desc->latency(); |
| 2290 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2291 | |
| 2292 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2293 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2294 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2295 | |
| 2296 | // Require mute check if another output is on a shared device |
| 2297 | // and currently active to have proper drain and avoid pops. |
| 2298 | // Note restoring AudioTracks onto this output needs to invoke |
| 2299 | // a volume ramp if there is no mute. |
| 2300 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2301 | } |
| 2302 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2303 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2304 | if (outputDesc->mUsePreferredMixerAttributes && devices != outputDesc->devices()) { |
| 2305 | // If the output is open with preferred mixer attributes, but the routed device is |
| 2306 | // changed when calling this function, returning DEAD_OBJECT to indicate routing |
| 2307 | // changed. |
| 2308 | return DEAD_OBJECT; |
| 2309 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2310 | const uint32_t muteWaitMs = |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2311 | setOutputDevices(outputDesc, devices, force, 0, nullptr, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2312 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2314 | auto &curves = getVolumeCurves(client->attributes()); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2315 | if (NO_ERROR != checkAndSetVolume(curves, client->volumeSource(), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2316 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2317 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2318 | outputDesc->devices().types(), 0 /*delay*/, |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2319 | outputDesc->useHwGain() /*force*/)) { |
| 2320 | // request AudioService to reinitialize the volume curves asynchronously |
| 2321 | ALOGE("checkAndSetVolume failed, requesting volume range init"); |
| 2322 | mpClientInterface->onVolumeRangeInitRequest(); |
| 2323 | }; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2324 | |
| 2325 | // update the outputs if starting an output with a stream that can affect notification |
| 2326 | // routing |
| 2327 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2328 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2329 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2330 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 2331 | invalidateStreams({AUDIO_STREAM_ACCESSIBILITY}); |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2332 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2333 | |
| 2334 | if (waitMs > muteWaitMs) { |
| 2335 | *delayMs = waitMs - muteWaitMs; |
| 2336 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2337 | |
| 2338 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2339 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2340 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2341 | // change occurs after the MixerThread starts and causes a stream volume |
| 2342 | // glitch. |
| 2343 | // |
| 2344 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2345 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2346 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2347 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2348 | mEngine->getForceUse( |
| 2349 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2350 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2351 | } |
| 2352 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2353 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2354 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2355 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2356 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2357 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2358 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2359 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2360 | "remote-submix", |
| 2361 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2362 | } |
| 2363 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2364 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, *delayMs); |
| 2365 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2366 | return NO_ERROR; |
| 2367 | } |
| 2368 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2369 | void AudioPolicyManager::checkLeBroadcastRoutes(bool wasUnicastActive, |
| 2370 | sp<SwAudioOutputDescriptor> ignoredOutput, uint32_t delayMs) { |
| 2371 | bool isUnicastActive = isLeUnicastActive(); |
| 2372 | |
| 2373 | if (wasUnicastActive != isUnicastActive) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2374 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2375 | //reroute all outputs routed to LE broadcast if LE unicast activy changed on any output |
| 2376 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2377 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2378 | if (desc != ignoredOutput && desc->isActive() |
| 2379 | && ((isUnicastActive && |
| 2380 | !desc->devices(). |
| 2381 | getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST).isEmpty()) |
| 2382 | || (wasUnicastActive && |
| 2383 | !desc->devices().getDevicesFromTypes( |
| 2384 | getAudioDeviceOutLeAudioUnicastSet()).isEmpty()))) { |
| 2385 | DeviceVector newDevices = getNewOutputDevices(desc, false /*fromCache*/); |
| 2386 | bool force = desc->devices() != newDevices; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2387 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2388 | // If the device is using preferred mixer attributes, the output need to reopen |
| 2389 | // with default configuration when the new selected devices are different from |
| 2390 | // current routing devices. |
| 2391 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 2392 | continue; |
| 2393 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2394 | setOutputDevices(desc, newDevices, force, delayMs); |
| 2395 | // re-apply device specific volume if not done by setOutputDevice() |
| 2396 | if (!force) { |
| 2397 | applyStreamVolumes(desc, newDevices.types(), delayMs); |
| 2398 | } |
| 2399 | } |
| 2400 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2401 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2402 | } |
| 2403 | } |
| 2404 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2405 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2406 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2407 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2408 | |
| 2409 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2410 | if (outputDesc == 0) { |
| 2411 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2412 | return BAD_VALUE; |
| 2413 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2414 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2415 | |
Jean-Michel Trivi | b1f6e64 | 2023-02-07 20:49:04 +0000 | [diff] [blame] | 2416 | if (client->hasPreferredDevice(true)) { |
| 2417 | // playback activity with preferred device impacts routing occurred, inform upper layers |
| 2418 | mpClientInterface->onRoutingUpdated(); |
| 2419 | } |
| 2420 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2421 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2422 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2423 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2424 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2425 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2426 | if (status == NO_ERROR ) { |
| 2427 | outputDesc->stop(); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2428 | } else { |
| 2429 | return status; |
| 2430 | } |
| 2431 | |
| 2432 | if (outputDesc->devices().size() == 1) { |
| 2433 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2434 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2435 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2436 | info->decreaseActiveClient(); |
| 2437 | if (info->getActiveClientCount() == 0) { |
| 2438 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2439 | } |
| 2440 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2441 | } |
| 2442 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2443 | } |
| 2444 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2445 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2446 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2447 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2448 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2449 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2450 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2451 | bool wasLeUnicastActive = isLeUnicastActive(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2452 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2453 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2454 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2455 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2456 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2457 | // Automatically disable the remote submix input when output is stopped on a |
| 2458 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2459 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2460 | if (isSingleDeviceType( |
| 2461 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2462 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2463 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2464 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2465 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2466 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2467 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2468 | } |
| 2469 | } |
| 2470 | bool forceDeviceUpdate = false; |
Eric Laurent | 72af801 | 2023-03-15 17:36:22 +0100 | [diff] [blame] | 2471 | if (client->hasPreferredDevice(true) && |
| 2472 | outputDesc->sameExclusivePreferredDevicesCount() < 2) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2473 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2474 | forceDeviceUpdate = true; |
| 2475 | } |
| 2476 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2477 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2478 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2479 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2480 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2481 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2482 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2483 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2484 | |
| 2485 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2486 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2487 | // volume group(s), force reapply volume |
| 2488 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2489 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2490 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2491 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2492 | // the track stop() command is received and at that time the audio track buffer can |
| 2493 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2494 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2495 | // audio path (audio DSP, CODEC ...) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2496 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2, |
| 2497 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2498 | |
| 2499 | // force restoring the device selection on other active outputs if it differs from the |
| 2500 | // one being selected for this output |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2501 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2502 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2503 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2504 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2505 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2506 | desc->isActive() && |
| 2507 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2508 | (newDevices != desc->devices())) { |
| 2509 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2510 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2511 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2512 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2513 | // If the device is using preferred mixer attributes, the output need to |
| 2514 | // reopen with default configuration when the new selected devices are |
| 2515 | // different from current routing devices. |
| 2516 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices2); |
| 2517 | continue; |
| 2518 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2519 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 2520 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2521 | // re-apply device specific volume if not done by setOutputDevice() |
| 2522 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2523 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2524 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2525 | } |
| 2526 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2527 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2528 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2529 | handleNotificationRoutingForStream(stream); |
| 2530 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2531 | |
| 2532 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2533 | 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] | 2534 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2535 | } |
| 2536 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2537 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2538 | selectOutputForMusicEffects(); |
| 2539 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2540 | |
| 2541 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, outputDesc->latency()*2); |
| 2542 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2543 | return NO_ERROR; |
| 2544 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2545 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2546 | return INVALID_OPERATION; |
| 2547 | } |
| 2548 | } |
| 2549 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2550 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2551 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2552 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2553 | |
| 2554 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2555 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2556 | // If an output descriptor is closed due to a device routing change, |
| 2557 | // then there are race conditions with releaseOutput from tracks |
| 2558 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2559 | // destroyed shortly thereafter. |
| 2560 | // |
| 2561 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2562 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2563 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2564 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2565 | |
| 2566 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2567 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2568 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2569 | if (outputDesc->isClientActive(client)) { |
| 2570 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2571 | stopOutput(portId); |
| 2572 | } |
| 2573 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2574 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2575 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2576 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2577 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2578 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2579 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2580 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2581 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2582 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2583 | } |
| 2584 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2585 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2586 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2587 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2588 | // The output is pending reopened to query dynamic profiles and |
| 2589 | // there is no active clients |
| 2590 | closeOutput(outputDesc->mIoHandle); |
| 2591 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2592 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2593 | if (newOutputDesc == nullptr) { |
| 2594 | ALOGE("%s failed to open output", __func__); |
| 2595 | } |
| 2596 | return true; |
| 2597 | } |
| 2598 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2601 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2602 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2603 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2604 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2605 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2606 | audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2607 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2608 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2609 | input_type_t *inputType, |
| 2610 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2611 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2612 | 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] | 2613 | "flags %#x attributes=%s requested device ID %d", |
| 2614 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2615 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2616 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2617 | status_t status = NO_ERROR; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2618 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2619 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2620 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2621 | sp<AudioInputDescriptor> inputDesc; |
| 2622 | sp<RecordClientDescriptor> clientDesc; |
| 2623 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2624 | 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] | 2625 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2626 | |
| 2627 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2628 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2629 | return INVALID_OPERATION; |
| 2630 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2631 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2632 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2633 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2634 | } |
| 2635 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2636 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2637 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2638 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2639 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2640 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2641 | // possible |
| 2642 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2643 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2644 | ssize_t index = mInputs.indexOfKey(*input); |
| 2645 | if (index < 0) { |
| 2646 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2647 | status = BAD_VALUE; |
| 2648 | goto error; |
| 2649 | } |
| 2650 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2651 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2652 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2653 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2654 | status = BAD_VALUE; |
| 2655 | goto error; |
| 2656 | } |
| 2657 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2658 | // 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] | 2659 | // corresponds to a new client and is only permitted from the same UID. |
| 2660 | // 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] | 2661 | if (clients.size() > 1) { |
| 2662 | for (const auto& client : clients) { |
| 2663 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2664 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2665 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2666 | // to an actual client |
| 2667 | if (client == *clients.cbegin()) { |
| 2668 | continue; |
| 2669 | } |
| 2670 | if (uid != client->uid() && !client->isSilenced()) { |
| 2671 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2672 | uid, client->portId(), client->uid()); |
| 2673 | status = INVALID_OPERATION; |
| 2674 | goto error; |
| 2675 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2676 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2677 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2678 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2679 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2680 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2681 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2682 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | *input = AUDIO_IO_HANDLE_NONE; |
| 2686 | *inputType = API_INPUT_INVALID; |
| 2687 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2688 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
Jan Sebechlebsky | bc56bcd | 2022-09-26 13:15:19 +0200 | [diff] [blame] | 2689 | extractAddressFromAudioAttributes(attributes).has_value()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2690 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2691 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2692 | ALOGW("%s could not find input mix for attr %s", |
| 2693 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2694 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2695 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2696 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2697 | String8(attr->tags + strlen("addr=")), |
| 2698 | AUDIO_FORMAT_DEFAULT); |
| 2699 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2700 | 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] | 2701 | __func__, attributes.source, attributes.tags); |
| 2702 | status = BAD_VALUE; |
| 2703 | goto error; |
| 2704 | } |
| 2705 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2706 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2707 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2708 | } else { |
| 2709 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2710 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2711 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2712 | if (explicitRoutingDevice != nullptr) { |
| 2713 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2714 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2715 | // Prevent from storing invalid requested device id in clients |
| 2716 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2717 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2718 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 2719 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2720 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2721 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2722 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2723 | status = BAD_VALUE; |
| 2724 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2725 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2726 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2727 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2728 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2729 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2730 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2731 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2732 | // know about it and is therefore considered "legacy" |
| 2733 | *inputType = API_INPUT_LEGACY; |
| 2734 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2735 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2736 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2737 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2738 | } else { |
| 2739 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2740 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2741 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2742 | } |
| 2743 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2744 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2745 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2746 | status = INVALID_OPERATION; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2747 | AudioProfileVector profiles; |
| 2748 | status_t ret = getProfilesForDevices( |
| 2749 | DeviceVector(device), profiles, flags, true /*isInput*/); |
| 2750 | if (ret == NO_ERROR && !profiles.empty()) { |
| 2751 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 2752 | : *profiles[0]->getChannels().begin(); |
| 2753 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 2754 | : *profiles[0]->getSampleRates().begin(); |
| 2755 | config->format = profiles[0]->getFormat(); |
| 2756 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2757 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2758 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2759 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2760 | exit: |
| 2761 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2762 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2763 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2764 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2765 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2766 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2767 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2768 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2769 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2770 | requestedDeviceId, attributes.source, flags, |
| 2771 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2772 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2773 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2774 | |
| 2775 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2776 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2777 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2778 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2779 | |
| 2780 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2781 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2785 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2786 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2787 | const audio_attributes_t &attributes, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2788 | audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2789 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2790 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2791 | { |
| 2792 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2793 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2794 | bool isSoundTrigger = false; |
| 2795 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2796 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2797 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2798 | if (index >= 0) { |
| 2799 | input = mSoundTriggerSessions.valueFor(session); |
| 2800 | isSoundTrigger = true; |
| 2801 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2802 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2803 | } else { |
| 2804 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2805 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2806 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2807 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2808 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 2811 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 2812 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 2813 | } |
| 2814 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2815 | // sampling rate and flags may be updated by getInputProfile |
| 2816 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2817 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2818 | audio_format_t profileFormat = config->format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2819 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2820 | audio_input_flags_t profileFlags = flags; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2821 | // find a compatible input profile (not necessarily identical in parameters) |
| 2822 | sp<IOProfile> profile = getInputProfile( |
| 2823 | device, profileSamplingRate, profileFormat, profileChannelMask, profileFlags); |
| 2824 | if (profile == nullptr) { |
| 2825 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2826 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2827 | |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2828 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2829 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2830 | if (samplingRate == 0) { |
| 2831 | samplingRate = profileSamplingRate; |
| 2832 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2833 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2834 | if (profile->getModuleHandle() == 0) { |
| 2835 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2836 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2837 | } |
| 2838 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2839 | // Reuse an already opened input if a client with the same session ID already exists |
| 2840 | // on that input |
| 2841 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2842 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2843 | if (desc->mProfile != profile) { |
| 2844 | continue; |
| 2845 | } |
| 2846 | RecordClientVector clients = desc->clientsList(); |
| 2847 | for (const auto &client : clients) { |
| 2848 | if (session == client->session()) { |
| 2849 | return desc->mIoHandle; |
| 2850 | } |
| 2851 | } |
| 2852 | } |
| 2853 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2854 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2855 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2856 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2857 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2858 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2859 | continue; |
| 2860 | } |
| 2861 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2862 | // else |
| 2863 | // reuse input if active client app is not in IDLE state |
| 2864 | // |
| 2865 | RecordClientVector clients = desc->clientsList(); |
| 2866 | bool doClose = false; |
| 2867 | for (const auto& client : clients) { |
| 2868 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2869 | continue; |
| 2870 | } |
| 2871 | if (client->isSoundTrigger()) { |
| 2872 | if (session == client->session()) { |
| 2873 | return desc->mIoHandle; |
| 2874 | } |
| 2875 | continue; |
| 2876 | } |
| 2877 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2878 | return desc->mIoHandle; |
| 2879 | } |
| 2880 | doClose = true; |
| 2881 | } |
| 2882 | if (doClose) { |
| 2883 | closeInput(desc->mIoHandle); |
| 2884 | } else { |
| 2885 | i++; |
| 2886 | } |
| 2887 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2888 | } |
| 2889 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2890 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2891 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2892 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2893 | lConfig.sample_rate = profileSamplingRate; |
| 2894 | lConfig.channel_mask = profileChannelMask; |
| 2895 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2896 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2897 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2898 | |
| 2899 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2900 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2901 | (profileSamplingRate != lConfig.sample_rate) || |
| 2902 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2903 | (profileChannelMask != lConfig.channel_mask)) { |
| 2904 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2905 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2906 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2907 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2908 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2909 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2910 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2911 | } |
| 2912 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2913 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2914 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2915 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2916 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2917 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2918 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2921 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2922 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2923 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2924 | |
| 2925 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2926 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2927 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2928 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2929 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2930 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2931 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2932 | if (client->active()) { |
| 2933 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2934 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2935 | } |
| 2936 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2937 | audio_session_t session = client->session(); |
| 2938 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2939 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2940 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2941 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2942 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2943 | status_t status = inputDesc->start(); |
| 2944 | if (status != NO_ERROR) { |
| 2945 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2946 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2947 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2948 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2949 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2950 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2951 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2952 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2953 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2954 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2955 | if (device != nullptr) { |
| 2956 | status = setInputDevice(input, device, true /* force */); |
| 2957 | } else { |
| 2958 | ALOGW("%s no new input device can be found for descriptor %d", |
| 2959 | __FUNCTION__, inputDesc->getId()); |
| 2960 | status = BAD_VALUE; |
| 2961 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2962 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2963 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2964 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2965 | // 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] | 2966 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2967 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2968 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2969 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2970 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2971 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2972 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2973 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2974 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2975 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2976 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2977 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2978 | // automatically enable the remote submix output when input is started if not |
| 2979 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2980 | // 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] | 2981 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2982 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2983 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2984 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2985 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2986 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2987 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2988 | if (address != "") { |
| 2989 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2990 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2991 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2992 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2993 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2994 | } else if (status != NO_ERROR) { |
| 2995 | // Restore client activity state. |
| 2996 | inputDesc->setClientActive(client, false); |
| 2997 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3000 | ALOGV("%s input %d source = %d status = %d exit", |
| 3001 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3002 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 3003 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3004 | } |
| 3005 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3006 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3007 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3008 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3009 | |
| 3010 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3011 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3012 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3013 | return BAD_VALUE; |
| 3014 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3015 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3016 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3017 | if (!client->active()) { |
| 3018 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3019 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3020 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3021 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3022 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3023 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3024 | inputDesc->stop(); |
| 3025 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 3026 | auto current_source = inputDesc->source(); |
| 3027 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 3028 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3029 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3030 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3031 | // 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] | 3032 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3033 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 3034 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3035 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 3036 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3037 | |
| 3038 | // automatically disable the remote submix output when input is stopped if not |
| 3039 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3040 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3041 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 3042 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3043 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 3044 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 3045 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3046 | } |
| 3047 | if (address != "") { |
| 3048 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3049 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3050 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3051 | } |
| 3052 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3053 | resetInputDevice(input); |
| 3054 | |
| 3055 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 3056 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3057 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3058 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3059 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3060 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3061 | } |
| 3062 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3063 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3064 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3067 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3068 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3069 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3070 | |
| 3071 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3072 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3073 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3074 | return; |
| 3075 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3076 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3077 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 3078 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3079 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3080 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3081 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3082 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3083 | if (inputDesc->getClientCount() > 0) { |
| 3084 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3085 | return; |
| 3086 | } |
| 3087 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3088 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3089 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3090 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3093 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3094 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3095 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3096 | |
| 3097 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3098 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3102 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 3103 | { |
| 3104 | stopInput(portId); |
| 3105 | releaseInput(portId); |
| 3106 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3107 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3108 | void AudioPolicyManager::checkCloseInputs() { |
| 3109 | // After connecting or disconnecting an input device, close input if: |
| 3110 | // - it has no client (was just opened to check profile) OR |
| 3111 | // - none of its supported devices are connected anymore OR |
| 3112 | // - one of its clients cannot be routed to one of its supported |
| 3113 | // devices anymore. Otherwise update device selection |
| 3114 | std::vector<audio_io_handle_t> inputsToClose; |
| 3115 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3116 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 3117 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 3118 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3119 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3120 | } else { |
| 3121 | bool close = false; |
| 3122 | for (const auto& client : input->clientsList()) { |
| 3123 | sp<DeviceDescriptor> device = |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 3124 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid(), |
| 3125 | client->session()); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3126 | if (!input->supportedDevices().contains(device)) { |
| 3127 | close = true; |
| 3128 | break; |
| 3129 | } |
| 3130 | } |
| 3131 | if (close) { |
| 3132 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3133 | } else { |
| 3134 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 3135 | } |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | for (const audio_io_handle_t handle : inputsToClose) { |
| 3140 | ALOGV("%s closing input %d", __func__, handle); |
| 3141 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3142 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3143 | } |
| 3144 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 3145 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3146 | { |
| 3147 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 3148 | if (indexMin < 0 || indexMax < 0) { |
| 3149 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 3150 | return; |
| 3151 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3152 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3153 | |
| 3154 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3155 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 3156 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3157 | continue; |
| 3158 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3159 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3160 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3163 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3164 | int index, |
| 3165 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3166 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3167 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 3168 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 3169 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 3170 | return NO_ERROR; |
| 3171 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3172 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 3173 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3174 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3177 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3178 | int *index, |
| 3179 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3180 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3181 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3182 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3183 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3184 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3185 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 3186 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3187 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3188 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3189 | } |
| 3190 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3191 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3192 | int index, |
| 3193 | audio_devices_t device) |
| 3194 | { |
| 3195 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3196 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 3197 | if (group == VOLUME_GROUP_NONE) { |
| 3198 | 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] | 3199 | return BAD_VALUE; |
| 3200 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3201 | ALOGV("%s: group %d matching with %s", __FUNCTION__, group, toString(attributes).c_str()); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3202 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3203 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3204 | VolumeSource vs = toVolumeSource(group); |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3205 | // AUDIO_STREAM_BLUETOOTH_SCO is only used for volume control so we remap |
| 3206 | // to AUDIO_STREAM_VOICE_CALL to match with relevant playback activity |
| 3207 | VolumeSource activityVs = (vs == toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false)) ? |
| 3208 | toVolumeSource(AUDIO_STREAM_VOICE_CALL, false) : vs; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3209 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 3210 | |
| 3211 | status = setVolumeCurveIndex(index, device, curves); |
| 3212 | if (status != NO_ERROR) { |
| 3213 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 3214 | return status; |
| 3215 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3216 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3217 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3218 | auto curCurvAttrs = curves.getAttributes(); |
| 3219 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 3220 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3221 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3222 | } else if (!curves.getStreamTypes().empty()) { |
| 3223 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3224 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3225 | } else { |
| 3226 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 3227 | return BAD_VALUE; |
| 3228 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3229 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 3230 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3231 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3232 | // update volume on all outputs and streams matching the following: |
| 3233 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 3234 | // - The device (or devices) selected by the engine for this stream includes |
| 3235 | // the requested device |
| 3236 | // - For non default requested device, currently selected device on the output is either the |
| 3237 | // requested device or one of the devices selected by the engine for this stream |
| 3238 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 3239 | // no specific device volume value exists for currently selected device. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3240 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3241 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3242 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3243 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3244 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 3245 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 3246 | } |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3247 | |
| 3248 | if (!(desc->isActive(activityVs) || isInCallOrScreening())) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3249 | continue; |
| 3250 | } |
| 3251 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 3252 | curDevices.find(device) == curDevices.end()) { |
| 3253 | continue; |
| 3254 | } |
| 3255 | bool applyVolume = false; |
| 3256 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 3257 | curSrcDevices.insert(device); |
| 3258 | applyVolume = (curSrcDevices.find( |
| 3259 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); |
| 3260 | } else { |
| 3261 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 3262 | } |
| 3263 | if (!applyVolume) { |
| 3264 | continue; // next output |
| 3265 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3266 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 3267 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 3268 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3269 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3270 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 3271 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 3272 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3273 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 3274 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 3275 | false /*preferredDevice*/); |
| 3276 | if (activeClients.empty()) { |
| 3277 | continue; |
| 3278 | } |
| 3279 | bool isPreempted = false; |
| 3280 | bool isHigherPriority = productStrategy < strategy; |
| 3281 | for (const auto &client : activeClients) { |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3282 | if (isHigherPriority && (client->volumeSource() != activityVs)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3283 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3284 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3285 | " higher priority source active:\n" |
| 3286 | " volumeGroup=%d attributes=%s) \n" |
| 3287 | " on output %zu, bailing out", __func__, productStrategy, |
| 3288 | group, group, toString(attributes).c_str(), |
| 3289 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3290 | applyVolume = false; |
| 3291 | isPreempted = true; |
| 3292 | break; |
| 3293 | } |
| 3294 | // 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] | 3295 | if (client->volumeSource() == activityVs) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3296 | applyVolume = true; |
| 3297 | } |
| 3298 | } |
| 3299 | if (isPreempted || applyVolume) { |
| 3300 | break; |
| 3301 | } |
| 3302 | } |
| 3303 | if (!applyVolume) { |
| 3304 | continue; // next output |
| 3305 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3306 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3307 | //FIXME: workaround for truncated touch sounds |
| 3308 | // delayed volume change for system stream to be removed when the problem is |
| 3309 | // handled by system UI |
| 3310 | status_t volStatus = checkAndSetVolume( |
| 3311 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3312 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3313 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3314 | if (volStatus != NO_ERROR) { |
| 3315 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3316 | } |
| 3317 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3318 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3319 | return status; |
| 3320 | } |
| 3321 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3322 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3323 | audio_devices_t device, |
| 3324 | IVolumeCurves &volumeCurves) |
| 3325 | { |
| 3326 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3327 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3328 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3329 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3330 | (index > volumeCurves.getVolumeIndexMax())) { |
| 3331 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 3332 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 3333 | return BAD_VALUE; |
| 3334 | } |
| 3335 | if (!audio_is_output_device(device)) { |
| 3336 | return BAD_VALUE; |
| 3337 | } |
| 3338 | |
| 3339 | // Force max volume if stream cannot be muted |
| 3340 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3341 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3342 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3343 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3344 | return NO_ERROR; |
| 3345 | } |
| 3346 | |
| 3347 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3348 | int &index, |
| 3349 | audio_devices_t device) |
| 3350 | { |
| 3351 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3352 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3353 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3354 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3355 | deviceTypes = mEngine->getOutputDevicesForAttributes( |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3356 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3357 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3358 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3362 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3363 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3364 | { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3365 | if (!isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3366 | return BAD_VALUE; |
| 3367 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3368 | index = curves.getVolumeIndex(deviceTypes); |
| 3369 | 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] | 3370 | return NO_ERROR; |
| 3371 | } |
| 3372 | |
| 3373 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3374 | int &index) |
| 3375 | { |
| 3376 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3377 | return NO_ERROR; |
| 3378 | } |
| 3379 | |
| 3380 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3381 | int &index) |
| 3382 | { |
| 3383 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3384 | return NO_ERROR; |
| 3385 | } |
| 3386 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3387 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3388 | { |
| 3389 | // select one output among several suitable for global effects. |
| 3390 | // The priority is as follows: |
| 3391 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3392 | // AudioFlinger will invalidate the track and the offloaded output |
| 3393 | // will be closed causing the effect to be moved to a PCM output. |
| 3394 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3395 | // 3: The primary output |
| 3396 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3397 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3398 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3399 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3400 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3401 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3402 | if (outputs.size() == 0) { |
| 3403 | return AUDIO_IO_HANDLE_NONE; |
| 3404 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3405 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3406 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3407 | bool activeOnly = true; |
| 3408 | |
| 3409 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3410 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 3411 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3412 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3413 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3414 | for (audio_io_handle_t output : outputs) { |
| 3415 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3416 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3417 | continue; |
| 3418 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3419 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 3420 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3421 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3422 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3423 | } |
| 3424 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3425 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3426 | } |
| 3427 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3428 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3429 | } |
| 3430 | } |
| 3431 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3432 | output = outputOffloaded; |
| 3433 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3434 | output = outputDeepBuffer; |
| 3435 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3436 | output = outputPrimary; |
| 3437 | } else { |
| 3438 | output = outputs[0]; |
| 3439 | } |
| 3440 | activeOnly = false; |
| 3441 | } |
| 3442 | |
| 3443 | if (output != mMusicEffectOutput) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3444 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3445 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 3446 | mMusicEffectOutput = output; |
| 3447 | } |
| 3448 | |
| 3449 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3450 | return output; |
| 3451 | } |
| 3452 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3453 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3454 | { |
| 3455 | return selectOutputForMusicEffects(); |
| 3456 | } |
| 3457 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3458 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3459 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3460 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3461 | int session, |
| 3462 | int id) |
| 3463 | { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3464 | if (session != AUDIO_SESSION_DEVICE) { |
| 3465 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3466 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3467 | index = mInputs.indexOfKey(io); |
| 3468 | if (index < 0) { |
| 3469 | ALOGW("registerEffect() unknown io %d", io); |
| 3470 | return INVALID_OPERATION; |
| 3471 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3472 | } |
| 3473 | } |
Eric Laurent | bf8f69f | 2022-03-25 17:48:38 +0100 | [diff] [blame] | 3474 | bool isMusicEffect = (session != AUDIO_SESSION_OUTPUT_STAGE) |
| 3475 | && ((strategy == streamToStrategy(AUDIO_STREAM_MUSIC) |
| 3476 | || strategy == PRODUCT_STRATEGY_NONE)); |
| 3477 | return mEffects.registerEffect(desc, io, session, id, isMusicEffect); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3478 | } |
| 3479 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3480 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3481 | { |
| 3482 | if (mEffects.getEffect(id) == nullptr) { |
| 3483 | return INVALID_OPERATION; |
| 3484 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3485 | if (mEffects.isEffectEnabled(id)) { |
| 3486 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3487 | setEffectEnabled(id, false); |
| 3488 | } |
| 3489 | return mEffects.unregisterEffect(id); |
| 3490 | } |
| 3491 | |
| 3492 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3493 | { |
| 3494 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3495 | if (effect == nullptr) { |
| 3496 | return INVALID_OPERATION; |
| 3497 | } |
| 3498 | |
| 3499 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3500 | if (status == NO_ERROR) { |
| 3501 | mInputs.trackEffectEnabled(effect, enabled); |
| 3502 | } |
| 3503 | return status; |
| 3504 | } |
| 3505 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3506 | |
| 3507 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3508 | { |
| 3509 | mEffects.moveEffects(ids, io); |
| 3510 | return NO_ERROR; |
| 3511 | } |
| 3512 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3513 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3514 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3515 | auto vs = toVolumeSource(stream, false); |
| 3516 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3520 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3521 | auto vs = toVolumeSource(stream, false); |
| 3522 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3523 | } |
| 3524 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3525 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3526 | { |
| 3527 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3528 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3529 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3530 | return true; |
| 3531 | } |
| 3532 | } |
| 3533 | return false; |
| 3534 | } |
| 3535 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3536 | // Register a list of custom mixes with their attributes and format. |
| 3537 | // When a mix is registered, corresponding input and output profiles are |
| 3538 | // added to the remote submix hw module. The profile contains only the |
| 3539 | // parameters (sampling rate, format...) specified by the mix. |
| 3540 | // The corresponding input remote submix device is also connected. |
| 3541 | // |
| 3542 | // When a remote submix device is connected, the address is checked to select the |
| 3543 | // appropriate profile and the corresponding input or output stream is opened. |
| 3544 | // |
| 3545 | // When capture starts, getInputForAttr() will: |
| 3546 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3547 | // - 2 if none found, getDeviceForInputSource() will: |
| 3548 | // - 2.1 look for a mix matching the attributes source |
| 3549 | // - 2.2 if none found, default to device selection by policy rules |
| 3550 | // At this time, the corresponding output remote submix device is also connected |
| 3551 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3552 | // after AudioTracks are invalidated |
| 3553 | // |
| 3554 | // When playback starts, getOutputForAttr() will: |
| 3555 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3556 | // - 2 if none found, look for a mix matching the attributes usage |
| 3557 | // - 3 if none found, default to device and output selection by policy rules. |
| 3558 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3559 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3560 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3561 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3562 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3563 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3564 | sp<HwModule> rSubmixModule; |
| 3565 | // examine each mix's route type |
| 3566 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3567 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3568 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3569 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3570 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3571 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3572 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3573 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3574 | break; |
| 3575 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3576 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3577 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3578 | 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] | 3579 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3580 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3581 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3582 | rSubmixModule = mHwModules.getModuleFromName( |
| 3583 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3584 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3585 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3586 | i); |
| 3587 | res = INVALID_OPERATION; |
| 3588 | break; |
| 3589 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3590 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3591 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3592 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3593 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3594 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3595 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3596 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3597 | } else { |
| 3598 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3599 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3600 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3601 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3602 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3603 | ALOGE("Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3604 | res = INVALID_OPERATION; |
| 3605 | break; |
| 3606 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3607 | audio_config_t outputConfig = mix.mFormat; |
| 3608 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3609 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3610 | // 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] | 3611 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3612 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3613 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3614 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3615 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3616 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3617 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3618 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3619 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3620 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
| 3621 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
| 3622 | mix.mDeviceType, address.string()); |
| 3623 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3624 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3625 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3626 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3627 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3628 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3629 | i, mixes.size(), type, address.string()); |
| 3630 | |
| 3631 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3632 | mix.mDeviceType, mix.mDeviceAddress, |
| 3633 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3634 | if (device == nullptr) { |
| 3635 | res = INVALID_OPERATION; |
| 3636 | break; |
| 3637 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3638 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3639 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3640 | // First try to find an already opened output supporting the device |
| 3641 | 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] | 3642 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3643 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3644 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3645 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3646 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3647 | address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3648 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3649 | } else { |
| 3650 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3651 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3652 | } |
| 3653 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3654 | // If no output found, try to find a direct output profile supporting the device |
| 3655 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3656 | sp<HwModule> module = mHwModules[i]; |
| 3657 | for (size_t j = 0; |
| 3658 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3659 | j++) { |
| 3660 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3661 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3662 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3663 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3664 | address.string()); |
| 3665 | res = INVALID_OPERATION; |
| 3666 | } else { |
| 3667 | foundOutput = true; |
| 3668 | } |
| 3669 | } |
| 3670 | } |
| 3671 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3672 | if (res != NO_ERROR) { |
| 3673 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3674 | i, type, address.string()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3675 | res = INVALID_OPERATION; |
| 3676 | break; |
| 3677 | } else if (!foundOutput) { |
| 3678 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3679 | i, type, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3680 | res = INVALID_OPERATION; |
| 3681 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3682 | } else { |
| 3683 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3684 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3685 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3686 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3687 | if (res != NO_ERROR) { |
| 3688 | unregisterPolicyMixes(mixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3689 | } else if (checkOutputs) { |
| 3690 | checkForDeviceAndOutputChanges(); |
| 3691 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3692 | } |
| 3693 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3697 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3698 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3699 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3700 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3701 | sp<HwModule> rSubmixModule; |
| 3702 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3703 | for (const auto& mix : mixes) { |
| 3704 | 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] | 3705 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3706 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3707 | rSubmixModule = mHwModules.getModuleFromName( |
| 3708 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3709 | if (rSubmixModule == 0) { |
| 3710 | res = INVALID_OPERATION; |
| 3711 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3712 | } |
| 3713 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3714 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3715 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3716 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3717 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3718 | res = INVALID_OPERATION; |
| 3719 | continue; |
| 3720 | } |
| 3721 | |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3722 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
| 3723 | if (getDeviceConnectionState(device, address.string()) == |
| 3724 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3725 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3726 | address.string(), "remote-submix", |
| 3727 | AUDIO_FORMAT_DEFAULT); |
| 3728 | if (res != OK) { |
| 3729 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
| 3730 | "with type %d, address %s", device, address.string()); |
| 3731 | } |
| 3732 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3733 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3734 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3735 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3736 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3737 | } 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] | 3738 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3739 | res = INVALID_OPERATION; |
| 3740 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3741 | } else { |
| 3742 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3743 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3744 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3745 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3746 | if (res == NO_ERROR && checkOutputs) { |
| 3747 | checkForDeviceAndOutputChanges(); |
| 3748 | updateCallAndOutputRouting(); |
| 3749 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3750 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3751 | } |
| 3752 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3753 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3754 | { |
| 3755 | size_t i = 0; |
| 3756 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3757 | for (const auto& fmt : mManualSurroundFormats) { |
| 3758 | if (i++ != 0) dst->append(", "); |
| 3759 | std::string sfmt; |
| 3760 | FormatConverter::toString(fmt, sfmt); |
| 3761 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3762 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3763 | } |
| 3764 | } |
| 3765 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3766 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3767 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3768 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3769 | std::function<bool(audio_devices_t)> predicate, |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3770 | const char *context, |
| 3771 | bool matchAddress) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3772 | for (size_t i = 0; i < devices.size(); i++) { |
| 3773 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3774 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3775 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, matchAddress); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3776 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3777 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3778 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3779 | return false; |
| 3780 | } |
| 3781 | } |
| 3782 | return true; |
| 3783 | } |
| 3784 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3785 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3786 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3787 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3788 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3789 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3790 | } |
| 3791 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3792 | if (res != NO_ERROR) { |
| 3793 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3794 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3795 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3796 | |
| 3797 | checkForDeviceAndOutputChanges(); |
| 3798 | updateCallAndOutputRouting(); |
| 3799 | |
| 3800 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 3804 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3805 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 3806 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3807 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3808 | __FUNCTION__, uid); |
| 3809 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3810 | } |
| 3811 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3812 | checkForDeviceAndOutputChanges(); |
| 3813 | updateCallAndOutputRouting(); |
| 3814 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3815 | return res; |
| 3816 | } |
| 3817 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3818 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3819 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 3820 | device_role_t role, |
| 3821 | const AudioDeviceTypeAddrVector &devices) { |
| 3822 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3823 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3824 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3825 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3826 | return BAD_VALUE; |
| 3827 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3828 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3829 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3830 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 3831 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3832 | return status; |
| 3833 | } |
| 3834 | |
| 3835 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3836 | |
| 3837 | bool forceVolumeReeval = false; |
| 3838 | // FIXME: workaround for truncated touch sounds |
| 3839 | // to be removed when the problem is handled by system UI |
| 3840 | uint32_t delayMs = 0; |
| 3841 | if (strategy == mCommunnicationStrategy) { |
| 3842 | forceVolumeReeval = true; |
| 3843 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3844 | updateInputRouting(); |
| 3845 | } |
| 3846 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3847 | |
| 3848 | return NO_ERROR; |
| 3849 | } |
| 3850 | |
| 3851 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs) |
| 3852 | { |
| 3853 | uint32_t waitMs = 0; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3854 | bool wasLeUnicastActive = isLeUnicastActive(); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 3855 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3856 | // Only apply special touch sound delay once |
| 3857 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3858 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3859 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3860 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3861 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3862 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3863 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 3864 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3865 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 3866 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 3867 | // Let's give back to engine full device choice decision however. |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3868 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3869 | if (outputDesc->mUsePreferredMixerAttributes && newDevices != outputDesc->devices()) { |
| 3870 | // If the device is using preferred mixer attributes, the output need to reopen |
| 3871 | // with default configuration when the new selected devices are different from |
| 3872 | // current routing devices. |
| 3873 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 3874 | continue; |
| 3875 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3876 | waitMs = setOutputDevices(outputDesc, newDevices, forceRouting, delayMs, nullptr, |
| 3877 | true /*requiresMuteCheck*/, |
| 3878 | !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3879 | // Only apply special touch sound delay once |
| 3880 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3881 | } |
| 3882 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 3883 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 3884 | } |
| 3885 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3886 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3887 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3890 | void AudioPolicyManager::updateInputRouting() { |
| 3891 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3892 | // Skip for hotword recording as the input device switch |
| 3893 | // is handled within sound trigger HAL |
| 3894 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 3895 | continue; |
| 3896 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3897 | auto newDevice = getNewInputDevice(activeDesc); |
| 3898 | // Force new input selection if the new device can not be reached via current input |
| 3899 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 3900 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 3901 | } else { |
| 3902 | closeInput(activeDesc->mIoHandle); |
| 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3907 | status_t |
| 3908 | AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 3909 | device_role_t role, |
| 3910 | const AudioDeviceTypeAddrVector &devices) { |
| 3911 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3912 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3913 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 3914 | if (!areAllDevicesSupported( |
| 3915 | devices, audio_is_output_device, __func__, /*matchAddress*/false)) { |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3916 | return BAD_VALUE; |
| 3917 | } |
| 3918 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role, devices); |
| 3919 | if (status != NO_ERROR) { |
| 3920 | ALOGW("Engine could not remove devices %s for strategy %d role %d", |
| 3921 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
| 3922 | return status; |
| 3923 | } |
| 3924 | |
| 3925 | checkForDeviceAndOutputChanges(); |
| 3926 | |
| 3927 | bool forceVolumeReeval = false; |
| 3928 | // TODO(b/263479999): workaround for truncated touch sounds |
| 3929 | // to be removed when the problem is handled by system UI |
| 3930 | uint32_t delayMs = 0; |
| 3931 | if (strategy == mCommunnicationStrategy) { |
| 3932 | forceVolumeReeval = true; |
| 3933 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3934 | updateInputRouting(); |
| 3935 | } |
| 3936 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
| 3937 | |
| 3938 | return NO_ERROR; |
| 3939 | } |
| 3940 | |
| 3941 | status_t AudioPolicyManager::clearDevicesRoleForStrategy(product_strategy_t strategy, |
| 3942 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3943 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3944 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3945 | |
Paul Wang | 5d7cdb5 | 2022-11-22 09:45:06 +0000 | [diff] [blame] | 3946 | status_t status = mEngine->clearDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3947 | if (status != NO_ERROR) { |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 3948 | ALOGW_IF(status != NAME_NOT_FOUND, |
| 3949 | "Engine could not remove device role for strategy %d status %d", |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3950 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3951 | return status; |
| 3952 | } |
| 3953 | |
| 3954 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3955 | |
| 3956 | bool forceVolumeReeval = false; |
| 3957 | // FIXME: workaround for truncated touch sounds |
| 3958 | // to be removed when the problem is handled by system UI |
| 3959 | uint32_t delayMs = 0; |
| 3960 | if (strategy == mCommunnicationStrategy) { |
| 3961 | forceVolumeReeval = true; |
| 3962 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3963 | updateInputRouting(); |
| 3964 | } |
| 3965 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3966 | |
| 3967 | return NO_ERROR; |
| 3968 | } |
| 3969 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3970 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 3971 | device_role_t role, |
| 3972 | AudioDeviceTypeAddrVector &devices) { |
| 3973 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3976 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 3977 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3978 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3979 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3980 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3981 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3982 | return BAD_VALUE; |
| 3983 | } |
| 3984 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3985 | ALOGW_IF(status != NO_ERROR, |
| 3986 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 3987 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3988 | |
| 3989 | return status; |
| 3990 | } |
| 3991 | |
| 3992 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 3993 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3994 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3995 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3996 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3997 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3998 | return BAD_VALUE; |
| 3999 | } |
| 4000 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 4001 | ALOGW_IF(status != NO_ERROR, |
| 4002 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 4003 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 4004 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4005 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4006 | return status; |
| 4007 | } |
| 4008 | |
| 4009 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 4010 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 4011 | { |
| 4012 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 4013 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 4014 | |
Eric Laurent | 78fedbf | 2023-03-09 14:40:44 +0100 | [diff] [blame] | 4015 | if (!areAllDevicesSupported( |
| 4016 | devices, audio_call_is_input_device, __func__, /*matchAddress*/false)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4017 | return BAD_VALUE; |
| 4018 | } |
| 4019 | |
| 4020 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 4021 | audioSource, role, devices); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4022 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4023 | "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] | 4024 | if (status == NO_ERROR) { |
| 4025 | updateInputRouting(); |
| 4026 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4027 | return status; |
| 4028 | } |
| 4029 | |
| 4030 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 4031 | device_role_t role) { |
| 4032 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 4033 | |
| 4034 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
Eric Laurent | 9ae44f3 | 2022-12-14 16:17:02 +0100 | [diff] [blame] | 4035 | ALOGW_IF(status != NO_ERROR && status != NAME_NOT_FOUND, |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4036 | "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] | 4037 | if (status == NO_ERROR) { |
| 4038 | updateInputRouting(); |
| 4039 | } |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 4040 | return status; |
| 4041 | } |
| 4042 | |
| 4043 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 4044 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 4045 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 4046 | } |
| 4047 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4048 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 4049 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4050 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4051 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 4052 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4053 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4054 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 4055 | if (status != NO_ERROR) { |
| 4056 | ALOGE("%s() could not set device affinity for userId %d", |
| 4057 | __FUNCTION__, userId); |
| 4058 | return status; |
| 4059 | } |
| 4060 | |
| 4061 | // reevaluate outputs for all devices |
| 4062 | checkForDeviceAndOutputChanges(); |
| 4063 | updateCallAndOutputRouting(); |
| 4064 | |
| 4065 | return NO_ERROR; |
| 4066 | } |
| 4067 | |
| 4068 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4069 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 4070 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 4071 | if (status != NO_ERROR) { |
| 4072 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 4073 | __FUNCTION__, userId); |
| 4074 | return status; |
| 4075 | } |
| 4076 | |
| 4077 | // reevaluate outputs for all devices |
| 4078 | checkForDeviceAndOutputChanges(); |
| 4079 | updateCallAndOutputRouting(); |
| 4080 | |
| 4081 | return NO_ERROR; |
| 4082 | } |
| 4083 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4084 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4085 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4086 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 4087 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4088 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 4089 | std::string stateLiteral; |
| 4090 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4091 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4092 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 4093 | "communications", "media", "record", "dock", "system", |
| 4094 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 4095 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 4096 | 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] | 4097 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 4098 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 4099 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 4100 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 4101 | dst->append(" (MANUAL: "); |
| 4102 | dumpManualSurroundFormats(dst); |
| 4103 | dst->append(")"); |
| 4104 | } |
| 4105 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4106 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4107 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 4108 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 4109 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4110 | dst->appendFormat(" Config source: %s\n", mConfig->getSource().c_str()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4111 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4112 | dst->append("\n"); |
| 4113 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 4114 | dst->append("\n"); |
| 4115 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4116 | mHwModules.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4117 | mOutputs.dump(dst); |
| 4118 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4119 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4120 | mAudioPatches.dump(dst); |
| 4121 | mPolicyMixes.dump(dst); |
| 4122 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4123 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4124 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 4125 | for (auto& policy : mAllowedCapturePolicies) { |
| 4126 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 4127 | } |
| 4128 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4129 | dst->appendFormat(" Preferred mixer audio configuration:\n"); |
| 4130 | for (const auto it : mPreferredMixerAttrInfos) { |
| 4131 | dst->appendFormat(" - device port id: %d\n", it.first); |
| 4132 | for (const auto preferredMixerInfoIt : it.second) { |
| 4133 | dst->appendFormat(" - strategy: %d; ", preferredMixerInfoIt.first); |
| 4134 | preferredMixerInfoIt.second->dump(dst); |
| 4135 | } |
| 4136 | } |
| 4137 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4138 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 4139 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | status_t AudioPolicyManager::dump(int fd) |
| 4143 | { |
| 4144 | String8 result; |
| 4145 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 4146 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4147 | return NO_ERROR; |
| 4148 | } |
| 4149 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4150 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 4151 | { |
| 4152 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 4153 | return NO_ERROR; |
| 4154 | } |
| 4155 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 | // This function checks for the parameters which can be offloaded. |
| 4157 | // This can be enhanced depending on the capability of the DSP and policy |
| 4158 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4159 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4160 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4161 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4162 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4163 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4164 | offloadInfo.format, |
| 4165 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 4166 | offloadInfo.has_video); |
| 4167 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4168 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4169 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | // See if there is a profile to support this. |
| 4173 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4174 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4175 | offloadInfo.sample_rate, |
| 4176 | offloadInfo.format, |
| 4177 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4178 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 4179 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 4180 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 4181 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 4182 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4183 | if (profile == nullptr) { |
| 4184 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 4185 | } |
| 4186 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 4187 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 4188 | } |
| 4189 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4190 | } |
| 4191 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4192 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 4193 | const audio_attributes_t& attributes) { |
| 4194 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 4195 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 4196 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 4197 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4198 | config.sample_rate, |
| 4199 | config.format, |
| 4200 | config.channel_mask, |
| 4201 | output_flags, |
| 4202 | true /* directOnly */); |
| 4203 | ALOGV("%s() profile %sfound with name: %s, " |
| 4204 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4205 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4206 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4207 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 4208 | |
| 4209 | // also try the MSD module if compatible profile not found |
| 4210 | if (profile == nullptr) { |
| 4211 | profile = getMsdProfileForOutput(outputDevices, |
| 4212 | config.sample_rate, |
| 4213 | config.format, |
| 4214 | config.channel_mask, |
| 4215 | output_flags, |
| 4216 | true /* directOnly */); |
| 4217 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 4218 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4219 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 4220 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 4221 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 4222 | } |
| 4223 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4224 | } |
| 4225 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4226 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 4227 | bool durationIgnored) { |
| 4228 | if (mMasterMono) { |
| 4229 | return false; // no offloading if mono is set. |
| 4230 | } |
| 4231 | |
| 4232 | // Check if offload has been disabled |
| 4233 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 4234 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 4235 | return false; |
| 4236 | } |
| 4237 | |
| 4238 | // Check if stream type is music, then only allow offload as of now. |
| 4239 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 4240 | { |
| 4241 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 4242 | return false; |
| 4243 | } |
| 4244 | |
| 4245 | //TODO: enable audio offloading with video when ready |
| 4246 | const bool allowOffloadWithVideo = |
| 4247 | property_get_bool("audio.offload.video", false /* default_value */); |
| 4248 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 4249 | ALOGV("%s: has_video == true, returning false", __func__); |
| 4250 | return false; |
| 4251 | } |
| 4252 | |
| 4253 | //If duration is less than minimum value defined in property, return false |
| 4254 | const int min_duration_secs = property_get_int32( |
| 4255 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 4256 | if (!durationIgnored) { |
| 4257 | if (min_duration_secs >= 0) { |
| 4258 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 4259 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 4260 | __func__, min_duration_secs); |
| 4261 | return false; |
| 4262 | } |
| 4263 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 4264 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 4265 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 4266 | return false; |
| 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 4271 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 4272 | // detects there is an active non offloadable effect. |
| 4273 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 4274 | // This may prevent offloading in rare situations where effects are left active by apps |
| 4275 | // in the background. |
| 4276 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4277 | return false; |
| 4278 | } |
| 4279 | |
| 4280 | return true; |
| 4281 | } |
| 4282 | |
| 4283 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 4284 | const audio_config_t *config) { |
| 4285 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 4286 | offloadInfo.format = config->format; |
| 4287 | offloadInfo.sample_rate = config->sample_rate; |
| 4288 | offloadInfo.channel_mask = config->channel_mask; |
| 4289 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 4290 | offloadInfo.has_video = false; |
| 4291 | offloadInfo.is_streaming = false; |
| 4292 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 4293 | |
| 4294 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 4295 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4296 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4297 | // only retain flags that will drive compressed offload or passthrough |
| 4298 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 4299 | if (offloadPossible) { |
| 4300 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 4301 | } |
| 4302 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 4303 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4304 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4305 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4306 | DeviceVector outputDevices = engineOutputDevices; |
| 4307 | // the MSD module checks for different conditions and output devices |
| 4308 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4309 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 4310 | continue; |
| 4311 | } |
| 4312 | outputDevices = getMsdAudioOutDevices(); |
| 4313 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4314 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | c8f7dfc | 2022-01-06 18:42:08 +0000 | [diff] [blame] | 4315 | if (!curProfile->isCompatibleProfile(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4316 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 4317 | config->format, nullptr /*updatedFormat*/, |
| 4318 | config->channel_mask, nullptr /*updatedChannelMask*/, |
| 4319 | flags)) { |
| 4320 | continue; |
| 4321 | } |
| 4322 | // reject profiles not corresponding to a device currently available |
| 4323 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 4324 | continue; |
| 4325 | } |
| 4326 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 4327 | != AUDIO_OUTPUT_FLAG_NONE) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 4328 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4329 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 4330 | // Already reports offload gapless supported. No need to report offload support. |
| 4331 | continue; |
| 4332 | } |
| 4333 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 4334 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 4335 | // If offload gapless is reported, no need to report offload support. |
| 4336 | directMode = (audio_direct_mode_t) ((directMode & |
| 4337 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 4338 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4339 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4340 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4341 | } |
| 4342 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4343 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | } |
| 4347 | return directMode; |
| 4348 | } |
| 4349 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4350 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4351 | AudioProfileVector& audioProfilesVector) { |
Dorin Drimus | 1711263 | 2022-09-23 15:28:51 +0000 | [diff] [blame] | 4352 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4353 | return OK; |
| 4354 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4355 | DeviceVector devices; |
| 4356 | status_t status = getDevicesForAttributes(*attr, devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4357 | if (status != OK) { |
| 4358 | return status; |
| 4359 | } |
| 4360 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4361 | if (devices.empty()) { |
| 4362 | return OK; // no output devices for the attributes |
| 4363 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4364 | return getProfilesForDevices(devices, audioProfilesVector, |
| 4365 | AUDIO_OUTPUT_FLAG_DIRECT /*flags*/, false /*isInput*/); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4368 | status_t AudioPolicyManager::getSupportedMixerAttributes( |
| 4369 | audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> &mixerAttrs) { |
| 4370 | ALOGV("%s, portId=%d", __func__, portId); |
| 4371 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4372 | if (deviceDescriptor == nullptr) { |
| 4373 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4374 | return BAD_VALUE; |
| 4375 | } |
| 4376 | for (const auto& hwModule : mHwModules) { |
| 4377 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4378 | if (curProfile->supportsDevice(deviceDescriptor)) { |
| 4379 | curProfile->toSupportedMixerAttributes(&mixerAttrs); |
| 4380 | } |
| 4381 | } |
| 4382 | } |
| 4383 | return NO_ERROR; |
| 4384 | } |
| 4385 | |
| 4386 | status_t AudioPolicyManager::setPreferredMixerAttributes( |
| 4387 | const audio_attributes_t *attr, |
| 4388 | audio_port_handle_t portId, |
| 4389 | uid_t uid, |
| 4390 | const audio_mixer_attributes_t *mixerAttributes) { |
| 4391 | ALOGV("%s, attr=%s, mixerAttributes={format=%#x, channelMask=%#x, samplingRate=%u, " |
| 4392 | "mixerBehavior=%d}, uid=%d, portId=%u", |
| 4393 | __func__, toString(*attr).c_str(), mixerAttributes->config.format, |
| 4394 | mixerAttributes->config.channel_mask, mixerAttributes->config.sample_rate, |
| 4395 | mixerAttributes->mixer_behavior, uid, portId); |
| 4396 | if (attr->usage != AUDIO_USAGE_MEDIA) { |
| 4397 | ALOGE("%s failed, only media is allowed, the given usage is %d", __func__, attr->usage); |
| 4398 | return BAD_VALUE; |
| 4399 | } |
| 4400 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4401 | if (deviceDescriptor == nullptr) { |
| 4402 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4403 | return BAD_VALUE; |
| 4404 | } |
| 4405 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4406 | ALOGE("%s(%d), type=%d, is not a usb output device", |
| 4407 | __func__, portId, deviceDescriptor->type()); |
| 4408 | return BAD_VALUE; |
| 4409 | } |
| 4410 | |
| 4411 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4412 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4413 | flags = (audio_output_flags_t) (flags | |
| 4414 | audio_output_flags_from_mixer_behavior(mixerAttributes->mixer_behavior)); |
| 4415 | sp<IOProfile> profile = nullptr; |
| 4416 | DeviceVector devices(deviceDescriptor); |
| 4417 | for (const auto& hwModule : mHwModules) { |
| 4418 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4419 | if (curProfile->hasDynamicAudioProfile() |
| 4420 | && curProfile->isCompatibleProfile(devices, |
| 4421 | mixerAttributes->config.sample_rate, |
| 4422 | nullptr /*updatedSamplingRate*/, |
| 4423 | mixerAttributes->config.format, |
| 4424 | nullptr /*updatedFormat*/, |
| 4425 | mixerAttributes->config.channel_mask, |
| 4426 | nullptr /*updatedChannelMask*/, |
| 4427 | flags, |
| 4428 | false /*exactMatchRequiredForInputFlags*/)) { |
| 4429 | profile = curProfile; |
| 4430 | break; |
| 4431 | } |
| 4432 | } |
| 4433 | } |
| 4434 | if (profile == nullptr) { |
| 4435 | ALOGE("%s, there is no compatible profile found", __func__); |
| 4436 | return BAD_VALUE; |
| 4437 | } |
| 4438 | |
| 4439 | sp<PreferredMixerAttributesInfo> mixerAttrInfo = |
| 4440 | sp<PreferredMixerAttributesInfo>::make( |
| 4441 | uid, portId, profile, flags, *mixerAttributes); |
| 4442 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4443 | mPreferredMixerAttrInfos[portId][strategy] = mixerAttrInfo; |
| 4444 | |
| 4445 | // If 1) there is any client from the preferred mixer configuration owner that is currently |
| 4446 | // active and matches the strategy and 2) current output is on the preferred device and the |
| 4447 | // mixer configuration doesn't match the preferred one, reopen output with preferred mixer |
| 4448 | // configuration. |
| 4449 | std::vector<audio_io_handle_t> outputsToReopen; |
| 4450 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4451 | const auto output = mOutputs.valueAt(i); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4452 | if (output->mProfile == profile && output->devices().onlyContainsDevice(deviceDescriptor)) { |
| 4453 | if (output->isConfigurationMatched(mixerAttributes->config, flags)) { |
| 4454 | output->mUsePreferredMixerAttributes = true; |
| 4455 | } else { |
| 4456 | for (const auto &client: output->getActiveClients()) { |
| 4457 | if (client->uid() == uid && client->strategy() == strategy) { |
| 4458 | client->setIsInvalid(); |
| 4459 | outputsToReopen.push_back(output->mIoHandle); |
| 4460 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4461 | } |
| 4462 | } |
| 4463 | } |
| 4464 | } |
| 4465 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4466 | config.sample_rate = mixerAttributes->config.sample_rate; |
| 4467 | config.channel_mask = mixerAttributes->config.channel_mask; |
| 4468 | config.format = mixerAttributes->config.format; |
| 4469 | for (const auto output : outputsToReopen) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4470 | sp<SwAudioOutputDescriptor> desc = |
| 4471 | reopenOutput(mOutputs.valueFor(output), &config, flags, __func__); |
| 4472 | if (desc == nullptr) { |
| 4473 | ALOGE("%s, failed to reopen output with preferred mixer attributes", __func__); |
| 4474 | continue; |
| 4475 | } |
| 4476 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | return NO_ERROR; |
| 4480 | } |
| 4481 | |
| 4482 | sp<PreferredMixerAttributesInfo> AudioPolicyManager::getPreferredMixerAttributesInfo( |
| 4483 | audio_port_handle_t devicePortId, product_strategy_t strategy) { |
| 4484 | auto it = mPreferredMixerAttrInfos.find(devicePortId); |
| 4485 | if (it == mPreferredMixerAttrInfos.end()) { |
| 4486 | return nullptr; |
| 4487 | } |
| 4488 | auto mixerAttrInfoIt = it->second.find(strategy); |
| 4489 | if (mixerAttrInfoIt == it->second.end()) { |
| 4490 | return nullptr; |
| 4491 | } |
| 4492 | return mixerAttrInfoIt->second; |
| 4493 | } |
| 4494 | |
| 4495 | status_t AudioPolicyManager::getPreferredMixerAttributes( |
| 4496 | const audio_attributes_t *attr, |
| 4497 | audio_port_handle_t portId, |
| 4498 | audio_mixer_attributes_t* mixerAttributes) { |
| 4499 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 4500 | portId, mEngine->getProductStrategyForAttributes(*attr)); |
| 4501 | if (info == nullptr) { |
| 4502 | return NAME_NOT_FOUND; |
| 4503 | } |
| 4504 | *mixerAttributes = info->getMixerAttributes(); |
| 4505 | return NO_ERROR; |
| 4506 | } |
| 4507 | |
| 4508 | status_t AudioPolicyManager::clearPreferredMixerAttributes(const audio_attributes_t *attr, |
| 4509 | audio_port_handle_t portId, |
| 4510 | uid_t uid) { |
| 4511 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4512 | const auto preferredMixerAttrInfo = getPreferredMixerAttributesInfo(portId, strategy); |
| 4513 | if (preferredMixerAttrInfo == nullptr) { |
| 4514 | return NAME_NOT_FOUND; |
| 4515 | } |
| 4516 | if (preferredMixerAttrInfo->getUid() != uid) { |
| 4517 | ALOGE("%s, requested uid=%d, owned uid=%d", |
| 4518 | __func__, uid, preferredMixerAttrInfo->getUid()); |
| 4519 | return PERMISSION_DENIED; |
| 4520 | } |
| 4521 | mPreferredMixerAttrInfos[portId].erase(strategy); |
| 4522 | if (mPreferredMixerAttrInfos[portId].empty()) { |
| 4523 | mPreferredMixerAttrInfos.erase(portId); |
| 4524 | } |
| 4525 | |
| 4526 | // Reconfig existing output |
| 4527 | std::vector<audio_io_handle_t> potentialOutputsToReopen; |
| 4528 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4529 | if (mOutputs.valueAt(i)->mProfile == preferredMixerAttrInfo->getProfile()) { |
| 4530 | potentialOutputsToReopen.push_back(mOutputs.keyAt(i)); |
| 4531 | } |
| 4532 | } |
| 4533 | for (const auto output : potentialOutputsToReopen) { |
| 4534 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 4535 | if (desc->isConfigurationMatched(preferredMixerAttrInfo->getConfigBase(), |
| 4536 | preferredMixerAttrInfo->getFlags())) { |
| 4537 | reopenOutput(desc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 4538 | } |
| 4539 | } |
| 4540 | return NO_ERROR; |
| 4541 | } |
| 4542 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4543 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 4544 | audio_port_type_t type, |
| 4545 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4546 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4547 | unsigned int *generation) |
| 4548 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4549 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 4550 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4551 | return BAD_VALUE; |
| 4552 | } |
| 4553 | 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] | 4554 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4555 | *num_ports = 0; |
| 4556 | } |
| 4557 | |
| 4558 | size_t portsWritten = 0; |
| 4559 | size_t portsMax = *num_ports; |
| 4560 | *num_ports = 0; |
| 4561 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4562 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 4563 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4564 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4565 | for (const auto& dev : mAvailableOutputDevices) { |
| 4566 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4567 | continue; |
| 4568 | } |
| 4569 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4570 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4571 | } |
| 4572 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4573 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4574 | } |
| 4575 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4576 | for (const auto& dev : mAvailableInputDevices) { |
| 4577 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4578 | continue; |
| 4579 | } |
| 4580 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4581 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4582 | } |
| 4583 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4584 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4585 | } |
| 4586 | } |
| 4587 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 4588 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 4589 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 4590 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4591 | } |
| 4592 | *num_ports += mInputs.size(); |
| 4593 | } |
| 4594 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4595 | size_t numOutputs = 0; |
| 4596 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4597 | if (!mOutputs[i]->isDuplicated()) { |
| 4598 | numOutputs++; |
| 4599 | if (portsWritten < portsMax) { |
| 4600 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4601 | } |
| 4602 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4603 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4604 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4605 | } |
| 4606 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4607 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4608 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4609 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4610 | return NO_ERROR; |
| 4611 | } |
| 4612 | |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 4613 | status_t AudioPolicyManager::listDeclaredDevicePorts(media::AudioPortRole role, |
| 4614 | std::vector<media::AudioPortFw>* _aidl_return) { |
| 4615 | auto pushPort = [&](const sp<DeviceDescriptor>& dev) -> status_t { |
| 4616 | audio_port_v7 port; |
| 4617 | dev->toAudioPort(&port); |
| 4618 | auto aidlPort = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_v7_AudioPortFw(port)); |
| 4619 | _aidl_return->push_back(std::move(aidlPort)); |
| 4620 | return OK; |
| 4621 | }; |
| 4622 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 4623 | for (const auto& module : mHwModules) { |
Mikhail Naganov | 5edc5ed | 2023-03-23 14:52:15 -0700 | [diff] [blame] | 4624 | for (const auto& dev : module->getDeclaredDevices()) { |
| 4625 | if (role == media::AudioPortRole::NONE || |
| 4626 | ((role == media::AudioPortRole::SOURCE) |
| 4627 | == audio_is_input_device(dev->type()))) { |
| 4628 | RETURN_STATUS_IF_ERROR(pushPort(dev)); |
| 4629 | } |
| 4630 | } |
| 4631 | } |
| 4632 | return OK; |
| 4633 | } |
| 4634 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4635 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4636 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 4637 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 4638 | return BAD_VALUE; |
| 4639 | } |
| 4640 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 4641 | if (dev != 0) { |
| 4642 | dev->toAudioPort(port); |
| 4643 | return NO_ERROR; |
| 4644 | } |
| 4645 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 4646 | if (dev != 0) { |
| 4647 | dev->toAudioPort(port); |
| 4648 | return NO_ERROR; |
| 4649 | } |
| 4650 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 4651 | if (out != 0) { |
| 4652 | out->toAudioPort(port); |
| 4653 | return NO_ERROR; |
| 4654 | } |
| 4655 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 4656 | if (in != 0) { |
| 4657 | in->toAudioPort(port); |
| 4658 | return NO_ERROR; |
| 4659 | } |
| 4660 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4661 | } |
| 4662 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4663 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 4664 | audio_patch_handle_t *handle, |
| 4665 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4666 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4667 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4668 | if (handle == NULL || patch == NULL) { |
| 4669 | return BAD_VALUE; |
| 4670 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4671 | 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] | 4672 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4673 | return BAD_VALUE; |
| 4674 | } |
| 4675 | // only one source per audio patch supported for now |
| 4676 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4677 | return INVALID_OPERATION; |
| 4678 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4679 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4680 | return INVALID_OPERATION; |
| 4681 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4682 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4683 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 4684 | return INVALID_OPERATION; |
| 4685 | } |
| 4686 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4687 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4688 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 4689 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 4690 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 4691 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 4692 | return BAD_VALUE; |
| 4693 | } |
| 4694 | ALOGV("%s between source %s and sink %s", __func__, |
| 4695 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 4696 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 4697 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 4698 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 4699 | const struct audio_port_config *source = &patch->sources[0]; |
| 4700 | sp<SourceClientDescriptor> sourceDesc = |
| 4701 | new InternalSourceClientDescriptor( |
| 4702 | portId, uid, attributes, *source, srcDevice, sinkDevice, |
| 4703 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes)); |
| 4704 | |
| 4705 | status_t status = |
| 4706 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 4707 | |
| 4708 | if (status != NO_ERROR) { |
| 4709 | return INVALID_OPERATION; |
| 4710 | } |
| 4711 | mAudioSources.add(portId, sourceDesc); |
| 4712 | return NO_ERROR; |
| 4713 | } |
| 4714 | |
| 4715 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 4716 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 4717 | const struct audio_patch *patch, |
| 4718 | audio_patch_handle_t &handle, |
| 4719 | uid_t uid, uint32_t delayMs) |
| 4720 | { |
| 4721 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 4722 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4723 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4724 | return INVALID_OPERATION; |
| 4725 | } |
| 4726 | sourceDesc->connect(handle, sinkDevice); |
| 4727 | if (isMsdPatch(handle)) { |
| 4728 | return NO_ERROR; |
| 4729 | } |
| 4730 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4731 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4732 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 4733 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 4734 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 4735 | goto FailurePatchAdded; |
| 4736 | } |
| 4737 | status = swOutput->start(); |
| 4738 | if (status != NO_ERROR) { |
| 4739 | goto FailureSourceAdded; |
| 4740 | } |
| 4741 | swOutput->addClient(sourceDesc); |
| 4742 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 4743 | if (status != NO_ERROR) { |
| 4744 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4745 | goto FailureSourceActive; |
| 4746 | } |
| 4747 | if (delayMs != 0) { |
| 4748 | usleep(delayMs * 1000); |
| 4749 | } |
| 4750 | return NO_ERROR; |
| 4751 | |
| 4752 | FailureSourceActive: |
| 4753 | swOutput->stop(); |
| 4754 | releaseOutput(sourceDesc->portId()); |
| 4755 | FailureSourceAdded: |
| 4756 | sourceDesc->setSwOutput(nullptr); |
| 4757 | FailurePatchAdded: |
| 4758 | releaseAudioPatchInternal(handle); |
| 4759 | return INVALID_OPERATION; |
| 4760 | } |
| 4761 | |
| 4762 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 4763 | audio_patch_handle_t *handle, |
| 4764 | uid_t uid, uint32_t delayMs, |
| 4765 | const sp<SourceClientDescriptor>& sourceDesc) |
| 4766 | { |
| 4767 | 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] | 4768 | sp<AudioPatch> patchDesc; |
| 4769 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 4770 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4771 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 4772 | patch->sources[0].role, |
| 4773 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4774 | #if LOG_NDEBUG == 0 |
| 4775 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4776 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 4777 | patch->sinks[i].role, |
| 4778 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4779 | } |
| 4780 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4781 | |
| 4782 | if (index >= 0) { |
| 4783 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4784 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 4785 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4786 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4787 | return INVALID_OPERATION; |
| 4788 | } |
| 4789 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4790 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4794 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4795 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4796 | 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] | 4797 | return BAD_VALUE; |
| 4798 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4799 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 4800 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4801 | if (patchDesc != 0) { |
| 4802 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4803 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 4804 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4805 | return BAD_VALUE; |
| 4806 | } |
| 4807 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4808 | DeviceVector devices; |
| 4809 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4810 | // Only support mix to devices connection |
| 4811 | // TODO add support for mix to mix connection |
| 4812 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4813 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4814 | return INVALID_OPERATION; |
| 4815 | } |
| 4816 | sp<DeviceDescriptor> devDesc = |
| 4817 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 4818 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4819 | 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] | 4820 | return BAD_VALUE; |
| 4821 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4822 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4823 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4824 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4825 | NULL, // updatedSamplingRate |
| 4826 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4827 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4828 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4829 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4830 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4831 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4832 | return INVALID_OPERATION; |
| 4833 | } |
| 4834 | devices.add(devDesc); |
| 4835 | } |
| 4836 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4837 | return INVALID_OPERATION; |
| 4838 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4839 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4840 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4841 | ALOGV("%s setting device %s on output %d", |
| 4842 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4843 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4844 | index = mAudioPatches.indexOfKey(*handle); |
| 4845 | if (index >= 0) { |
| 4846 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4847 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4848 | } |
| 4849 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4850 | patchDesc->setUid(uid); |
| 4851 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4852 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4853 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4854 | return INVALID_OPERATION; |
| 4855 | } |
| 4856 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4857 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 4858 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4859 | // only one sink supported when connecting an input device to a mix |
| 4860 | if (patch->num_sinks > 1) { |
| 4861 | return INVALID_OPERATION; |
| 4862 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4863 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4864 | if (inputDesc == NULL) { |
| 4865 | return BAD_VALUE; |
| 4866 | } |
| 4867 | if (patchDesc != 0) { |
| 4868 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 4869 | return BAD_VALUE; |
| 4870 | } |
| 4871 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4872 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4873 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4874 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4875 | return BAD_VALUE; |
| 4876 | } |
| 4877 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4878 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4879 | patch->sinks[0].sample_rate, |
| 4880 | NULL, /*updatedSampleRate*/ |
| 4881 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4882 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4883 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4884 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4885 | // FIXME for the parameter type, |
| 4886 | // and the NONE |
| 4887 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4888 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4889 | return INVALID_OPERATION; |
| 4890 | } |
| 4891 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4892 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4893 | device->toString().c_str(), inputDesc->mIoHandle); |
| 4894 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4895 | index = mAudioPatches.indexOfKey(*handle); |
| 4896 | if (index >= 0) { |
| 4897 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4898 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4899 | } |
| 4900 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4901 | patchDesc->setUid(uid); |
| 4902 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4903 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4904 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4905 | return INVALID_OPERATION; |
| 4906 | } |
| 4907 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4908 | // device to device connection |
| 4909 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4910 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4911 | return BAD_VALUE; |
| 4912 | } |
| 4913 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4914 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4915 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4916 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 4917 | return BAD_VALUE; |
| 4918 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4919 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4920 | //update source and sink with our own data as the data passed in the patch may |
| 4921 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4922 | PatchBuilder patchBuilder; |
| 4923 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4924 | |
| 4925 | // if first sink is to MSD, establish single MSD patch |
| 4926 | if (getMsdAudioOutDevices().contains( |
| 4927 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 4928 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 4929 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 4930 | goto installPatch; |
| 4931 | } |
| 4932 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4933 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 4934 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4935 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4936 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4937 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4938 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4939 | return INVALID_OPERATION; |
| 4940 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4941 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4942 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4943 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4944 | return BAD_VALUE; |
| 4945 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4946 | audio_port_config sinkPortConfig = {}; |
| 4947 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 4948 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4949 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4950 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 4951 | // volume management purpose (tracking activity) |
| 4952 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 4953 | // in config XML to reach the sink so that is can be declared as available. |
| 4954 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 4955 | sp<SwAudioOutputDescriptor> outputDesc; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4956 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4957 | // take care of dynamic routing for SwOutput selection, |
| 4958 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 4959 | audio_stream_type_t stream = sourceDesc->stream(); |
| 4960 | audio_attributes_t resultAttr; |
| 4961 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4962 | config.sample_rate = sourceDesc->config().sample_rate; |
François Gaffie | 2a24db4 | 2022-04-04 16:45:02 +0200 | [diff] [blame] | 4963 | audio_channel_mask_t sourceMask = sourceDesc->config().channel_mask; |
| 4964 | config.channel_mask = |
| 4965 | (audio_channel_mask_get_representation(sourceMask) |
| 4966 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) ? sourceMask |
| 4967 | : audio_channel_mask_in_to_out(sourceMask); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4968 | config.format = sourceDesc->config().format; |
| 4969 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4970 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 4971 | bool isRequestedDeviceForExclusiveUse = false; |
| 4972 | output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4973 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4974 | bool isBitPerfect; |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4975 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 4976 | &stream, sourceDesc->uid(), &config, &flags, |
| 4977 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4978 | nullptr, &outputType, &isSpatialized, &isBitPerfect); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4979 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4980 | ALOGV("%s no output for device %s", |
| 4981 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 4982 | return INVALID_OPERATION; |
| 4983 | } |
| 4984 | outputDesc = mOutputs.valueFor(output); |
| 4985 | if (outputDesc->isDuplicated()) { |
| 4986 | ALOGE("%s output is duplicated", __func__); |
| 4987 | return INVALID_OPERATION; |
| 4988 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 4989 | bool closeOutput = outputDesc->mDirectOpenCount != 0; |
| 4990 | sourceDesc->setSwOutput(outputDesc, closeOutput); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4991 | } else { |
| 4992 | // Same for "raw patches" aka created from createAudioPatch API |
| 4993 | SortedVector<audio_io_handle_t> outputs = |
| 4994 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 4995 | // if the sink device is reachable via an opened output stream, request to |
| 4996 | // go via this output stream by adding a second source to the patch |
| 4997 | // description |
| 4998 | output = selectOutput(outputs); |
| 4999 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 5000 | ALOGE("%s no output available for internal patch sink", __func__); |
| 5001 | return INVALID_OPERATION; |
| 5002 | } |
| 5003 | outputDesc = mOutputs.valueFor(output); |
| 5004 | if (outputDesc->isDuplicated()) { |
| 5005 | ALOGV("%s output for device %s is duplicated", |
| 5006 | __func__, sinkDevice->toString().c_str()); |
| 5007 | return INVALID_OPERATION; |
| 5008 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5009 | sourceDesc->setSwOutput(outputDesc, /* closeOutput= */ false); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5010 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5011 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 5012 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5013 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 5014 | // - 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] | 5015 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 5016 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5017 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 5018 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5019 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5020 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5021 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 5022 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5023 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5024 | return INVALID_OPERATION; |
| 5025 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5026 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5027 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5028 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 5029 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5030 | // for volume control, we may need a valid stream |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 5031 | srcMixPortConfig.ext.mix.usecase.stream = |
| 5032 | (!sourceDesc->isInternal() || isCallTxAudioSource(sourceDesc)) ? |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5033 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 5034 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5035 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 5036 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 5037 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5038 | } |
| 5039 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 5040 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5041 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5042 | status_t status = installPatch( |
| 5043 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5044 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5045 | 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] | 5046 | return INVALID_OPERATION; |
| 5047 | } |
| 5048 | } else { |
| 5049 | return BAD_VALUE; |
| 5050 | } |
| 5051 | } else { |
| 5052 | return BAD_VALUE; |
| 5053 | } |
| 5054 | return NO_ERROR; |
| 5055 | } |
| 5056 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5057 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5058 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5059 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5060 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 5061 | |
| 5062 | if (index < 0) { |
| 5063 | return BAD_VALUE; |
| 5064 | } |
| 5065 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5066 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 5067 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 5068 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5069 | return INVALID_OPERATION; |
| 5070 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5071 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5072 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 5073 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5074 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 5075 | portId = sourceDesc->portId(); |
| 5076 | break; |
| 5077 | } |
| 5078 | } |
| 5079 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 5080 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5081 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5082 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5083 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5084 | uint32_t delayMs, |
| 5085 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5086 | { |
| 5087 | ALOGV("%s patch %d", __func__, handle); |
| 5088 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 5089 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 5090 | return BAD_VALUE; |
| 5091 | } |
| 5092 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5093 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5094 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5095 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5096 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5097 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5098 | 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] | 5099 | return BAD_VALUE; |
| 5100 | } |
| 5101 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5102 | setOutputDevices(outputDesc, |
| 5103 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 5104 | true, |
| 5105 | 0, |
| 5106 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5107 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5108 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5109 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5110 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5111 | 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] | 5112 | return BAD_VALUE; |
| 5113 | } |
| 5114 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5115 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5116 | true, |
| 5117 | NULL); |
| 5118 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5119 | status_t status = |
| 5120 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 5121 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 5122 | __func__, status, patchDesc->getAfHandle()); |
| 5123 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5124 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5125 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5126 | // SW or HW Bridge |
| 5127 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 5128 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 5129 | 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] | 5130 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 5131 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 5132 | outputDesc = sourceDesc->swOutput().promote(); |
| 5133 | } |
| 5134 | if (outputDesc == nullptr) { |
| 5135 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 5136 | // releaseOutput has already called closeOutput in case of direct output |
| 5137 | return NO_ERROR; |
| 5138 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5139 | patchHandle = outputDesc->getPatchHandle(); |
| 5140 | // When a Sw bridge is released, the mixer used by this bridge will release its |
| 5141 | // patch at AudioFlinger side. Hence, the mixer audio patch must be recreated |
| 5142 | // Reuse patch handle to force audio flinger removing initial mixer patch removal |
| 5143 | // updating hal patch handle (prevent leaks). |
| 5144 | // While using a HwBridge, force reconsidering device only if not reusing an existing |
| 5145 | // output and no more activity on output (will force to close). |
| 5146 | bool force = sourceDesc->useSwBridge() || |
| 5147 | (sourceDesc->canCloseOutput() && !outputDesc->isActive()); |
| 5148 | // APM pattern is to have always outputs opened / patch realized for reachable devices. |
| 5149 | // Update device may result to NONE (empty), coupled with force, it releases the patch. |
| 5150 | // Reconsider device only for cases: |
| 5151 | // 1 / Active Output |
| 5152 | // 2 / Inactive Output previously hosting HwBridge |
| 5153 | // 3 / Inactive Output previously hosting SwBridge that can be closed. |
| 5154 | bool updateDevice = outputDesc->isActive() || !sourceDesc->useSwBridge() || |
| 5155 | sourceDesc->canCloseOutput(); |
| 5156 | setOutputDevices(outputDesc, |
| 5157 | updateDevice ? getNewOutputDevices(outputDesc, true /*fromCache*/) : |
| 5158 | outputDesc->devices(), |
| 5159 | force, |
| 5160 | 0, |
| 5161 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5162 | } else { |
| 5163 | return BAD_VALUE; |
| 5164 | } |
| 5165 | } else { |
| 5166 | return BAD_VALUE; |
| 5167 | } |
| 5168 | return NO_ERROR; |
| 5169 | } |
| 5170 | |
| 5171 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 5172 | struct audio_patch *patches, |
| 5173 | unsigned int *generation) |
| 5174 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5175 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5176 | return BAD_VALUE; |
| 5177 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5178 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5179 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5180 | } |
| 5181 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5182 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5183 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5184 | ALOGV("setAudioPortConfig()"); |
| 5185 | |
| 5186 | if (config == NULL) { |
| 5187 | return BAD_VALUE; |
| 5188 | } |
| 5189 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 5190 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5191 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 5192 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5193 | } |
| 5194 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5195 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5196 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 5197 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5198 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5199 | if (outputDesc == NULL) { |
| 5200 | return BAD_VALUE; |
| 5201 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5202 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 5203 | "setAudioPortConfig() called on duplicated output %d", |
| 5204 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5205 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5206 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5207 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5208 | if (inputDesc == NULL) { |
| 5209 | return BAD_VALUE; |
| 5210 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5211 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5212 | } else { |
| 5213 | return BAD_VALUE; |
| 5214 | } |
| 5215 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 5216 | sp<DeviceDescriptor> deviceDesc; |
| 5217 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 5218 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 5219 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 5220 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 5221 | } else { |
| 5222 | return BAD_VALUE; |
| 5223 | } |
| 5224 | if (deviceDesc == NULL) { |
| 5225 | return BAD_VALUE; |
| 5226 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5227 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5228 | } else { |
| 5229 | return BAD_VALUE; |
| 5230 | } |
| 5231 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5232 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5233 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 5234 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5235 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5236 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 5237 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5238 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5239 | if (status != NO_ERROR) { |
| 5240 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5241 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5242 | |
| 5243 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5244 | } |
| 5245 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5246 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 5247 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5248 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5249 | clearAudioPatches(uid); |
| 5250 | clearSessionRoutes(uid); |
| 5251 | } |
| 5252 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5253 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 5254 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5255 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5256 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5257 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5258 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5259 | } |
| 5260 | } |
| 5261 | } |
| 5262 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5263 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5264 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5265 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 5266 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 5267 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 5268 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5269 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5270 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5271 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 5272 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 5273 | continue; |
| 5274 | } |
| 5275 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5276 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5277 | continue; |
| 5278 | } |
| 5279 | // If the default device for this strategy is on another output mix, |
| 5280 | // invalidate all tracks in this strategy to force re connection. |
| 5281 | // Otherwise select new device on the output mix. |
| 5282 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5283 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(ps)); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5284 | } else { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5285 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 5286 | if (outputDesc->mUsePreferredMixerAttributes && outputDesc->devices() != newDevices) { |
| 5287 | // If the device is using preferred mixer attributes, the output need to reopen |
| 5288 | // with default configuration when the new selected devices are different from |
| 5289 | // current routing devices. |
| 5290 | outputsToReopen.emplace(mOutputs.keyAt(j), newDevices); |
| 5291 | continue; |
| 5292 | } |
| 5293 | setOutputDevices(outputDesc, newDevices, false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5294 | } |
| 5295 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5296 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 5300 | { |
| 5301 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5302 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5303 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5304 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5305 | for (const auto& client : outputDesc->getClientIterable()) { |
| 5306 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5307 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5308 | auto clientStrategy = client->strategy(); |
| 5309 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 5310 | end(affectedStrategies)) { |
| 5311 | continue; |
| 5312 | } |
| 5313 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5314 | } |
| 5315 | } |
| 5316 | } |
| 5317 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5318 | for (const auto& strategy : affectedStrategies) { |
| 5319 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | // remove input routes associated with this uid |
| 5323 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5324 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5325 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5326 | for (const auto& client : inputDesc->getClientIterable()) { |
| 5327 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5328 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 5329 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5330 | } |
| 5331 | } |
| 5332 | } |
| 5333 | // reroute inputs if necessary |
| 5334 | SortedVector<audio_io_handle_t> inputsToClose; |
| 5335 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5336 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5337 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5338 | inputsToClose.add(inputDesc->mIoHandle); |
| 5339 | } |
| 5340 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5341 | for (const auto& input : inputsToClose) { |
| 5342 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5343 | } |
| 5344 | } |
| 5345 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5346 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 5347 | { |
| 5348 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5349 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5350 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5351 | stopAudioSource(mAudioSources.keyAt(i)); |
| 5352 | } |
| 5353 | } |
| 5354 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5355 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5356 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 5357 | audio_io_handle_t *ioHandle, |
| 5358 | audio_devices_t *device) |
| 5359 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 5360 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 5361 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5362 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
Eric Laurent | cedd5b5 | 2023-03-22 00:03:31 +0000 | [diff] [blame] | 5363 | sp<DeviceDescriptor> deviceDesc = mEngine->getInputDeviceForAttributes(attr); |
| 5364 | if (deviceDesc == nullptr) { |
| 5365 | return INVALID_OPERATION; |
| 5366 | } |
| 5367 | *device = deviceDesc->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5368 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 5369 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5370 | } |
| 5371 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5372 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5373 | const audio_attributes_t *attributes, |
| 5374 | audio_port_handle_t *portId, |
| 5375 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5376 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5377 | ALOGV("%s", __FUNCTION__); |
| 5378 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 5379 | |
| 5380 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 5381 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 5382 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5383 | return BAD_VALUE; |
| 5384 | } |
| 5385 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5386 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 5387 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5388 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 5389 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5390 | return INVALID_OPERATION; |
| 5391 | } |
| 5392 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5393 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5394 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5395 | String8(source->ext.device.address), |
| 5396 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5397 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5398 | 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] | 5399 | return BAD_VALUE; |
| 5400 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5401 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5402 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5403 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5404 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5405 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5406 | mEngine->getStreamTypeForAttributes(*attributes), |
| 5407 | mEngine->getProductStrategyForAttributes(*attributes), |
| 5408 | toVolumeSource(*attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5409 | |
| 5410 | status_t status = connectAudioSource(sourceDesc); |
| 5411 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5412 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5413 | } |
| 5414 | return status; |
| 5415 | } |
| 5416 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 5417 | sp<SourceClientDescriptor> AudioPolicyManager::startAudioSourceInternal( |
| 5418 | const struct audio_port_config *source, const audio_attributes_t *attributes, uid_t uid) |
| 5419 | { |
| 5420 | ALOGV("%s", __FUNCTION__); |
| 5421 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5422 | |
| 5423 | status_t status = startAudioSource(source, attributes, &portId, uid); |
| 5424 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 5425 | return mAudioSources.valueFor(portId); |
| 5426 | } |
| 5427 | |
| 5428 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5429 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5430 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5431 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5432 | |
| 5433 | // make sure we only have one patch per source. |
| 5434 | disconnectAudioSource(sourceDesc); |
| 5435 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5436 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5437 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 5438 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 5439 | sourceDesc->srcDevice()->type(), |
| 5440 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 5441 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5442 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5443 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5444 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5445 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5446 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 5447 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 5448 | return INVALID_OPERATION; |
| 5449 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5450 | PatchBuilder patchBuilder; |
| 5451 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 5452 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5453 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5454 | return connectAudioSourceToSink( |
| 5455 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, 0 /*delayMs*/); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5456 | } |
| 5457 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5458 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5459 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5460 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 5461 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5462 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5463 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5464 | return BAD_VALUE; |
| 5465 | } |
| 5466 | status_t status = disconnectAudioSource(sourceDesc); |
| 5467 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5468 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5469 | return status; |
| 5470 | } |
| 5471 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5472 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 5473 | { |
| 5474 | if (mMasterMono == mono) { |
| 5475 | return NO_ERROR; |
| 5476 | } |
| 5477 | mMasterMono = mono; |
| 5478 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 5479 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 5480 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 5481 | // |
| 5482 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 5483 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 5484 | // play back offloaded. |
| 5485 | if (mMasterMono) { |
| 5486 | Vector<audio_io_handle_t> offloaded; |
| 5487 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5488 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5489 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 5490 | offloaded.push(desc->mIoHandle); |
| 5491 | } |
| 5492 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5493 | for (const auto& handle : offloaded) { |
| 5494 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5495 | } |
| 5496 | } |
| 5497 | // update master mono for all remaining outputs |
| 5498 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5499 | updateMono(mOutputs.keyAt(i)); |
| 5500 | } |
| 5501 | return NO_ERROR; |
| 5502 | } |
| 5503 | |
| 5504 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 5505 | { |
| 5506 | *mono = mMasterMono; |
| 5507 | return NO_ERROR; |
| 5508 | } |
| 5509 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5510 | float AudioPolicyManager::getStreamVolumeDB( |
| 5511 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 5512 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5513 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5514 | } |
| 5515 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5516 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 5517 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5518 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5519 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5520 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 5521 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5522 | return BAD_VALUE; |
| 5523 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5524 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 5525 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5526 | |
| 5527 | size_t formatsWritten = 0; |
| 5528 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 5529 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5530 | *numSurroundFormats = mConfig->getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5531 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5532 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5533 | for (const auto& format: mConfig->getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5534 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5535 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5536 | bool formatEnabled = true; |
| 5537 | switch (forceUse) { |
| 5538 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5539 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5540 | break; |
| 5541 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 5542 | formatEnabled = false; |
| 5543 | break; |
| 5544 | default: // AUTO or ALWAYS => true |
| 5545 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5546 | } |
| 5547 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 5548 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5549 | } |
| 5550 | return NO_ERROR; |
| 5551 | } |
| 5552 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5553 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 5554 | audio_format_t *surroundFormats) { |
| 5555 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 5556 | return BAD_VALUE; |
| 5557 | } |
| 5558 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 5559 | __func__, *numSurroundFormats, surroundFormats); |
| 5560 | |
| 5561 | size_t formatsWritten = 0; |
| 5562 | size_t formatsMax = *numSurroundFormats; |
| 5563 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 5564 | |
| 5565 | // Return formats from all device profiles that have already been resolved by |
| 5566 | // checkOutputsForDevice(). |
| 5567 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 5568 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 5569 | audio_devices_t deviceType = device->type(); |
| 5570 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 5571 | // returns formats reported by HDMI devices. |
| 5572 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 5573 | continue; |
| 5574 | } |
| 5575 | // Formats reported by sink devices |
| 5576 | std::unordered_set<audio_format_t> formatset; |
| 5577 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 5578 | formatset.insert(it->second.begin(), it->second.end()); |
| 5579 | } |
| 5580 | |
| 5581 | // Formats hard-coded in the in policy configuration file (if any). |
| 5582 | FormatVector encodedFormats = device->encodedFormats(); |
| 5583 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 5584 | // Filter the formats which are supported by the vendor hardware. |
| 5585 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5586 | if (mConfig->getSurroundFormats().count(*it) != 0) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5587 | formats.insert(*it); |
| 5588 | } else { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5589 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5590 | if (pair.second.count(*it) != 0) { |
| 5591 | formats.insert(pair.first); |
| 5592 | break; |
| 5593 | } |
| 5594 | } |
| 5595 | } |
| 5596 | } |
| 5597 | } |
| 5598 | *numSurroundFormats = formats.size(); |
| 5599 | for (const auto& format: formats) { |
| 5600 | if (formatsWritten < formatsMax) { |
| 5601 | surroundFormats[formatsWritten++] = format; |
| 5602 | } |
| 5603 | } |
| 5604 | return NO_ERROR; |
| 5605 | } |
| 5606 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5607 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 5608 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5609 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5610 | const auto& formatIter = mConfig->getSurroundFormats().find(audioFormat); |
| 5611 | if (formatIter == mConfig->getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5612 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5613 | return BAD_VALUE; |
| 5614 | } |
| 5615 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5616 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 5617 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5618 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5619 | return INVALID_OPERATION; |
| 5620 | } |
| 5621 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5622 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5623 | return NO_ERROR; |
| 5624 | } |
| 5625 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5626 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5627 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5628 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5629 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5630 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5631 | } |
| 5632 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5633 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5634 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5635 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5636 | } |
| 5637 | } |
| 5638 | |
| 5639 | sp<SwAudioOutputDescriptor> outputDesc; |
| 5640 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5641 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 5642 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5643 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 5644 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5645 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5646 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5647 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5648 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5649 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5650 | name.c_str(), |
| 5651 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5652 | if (status != NO_ERROR) { |
| 5653 | continue; |
| 5654 | } |
| 5655 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5656 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5657 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5658 | name.c_str(), |
| 5659 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5660 | profileUpdated |= (status == NO_ERROR); |
| 5661 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5662 | // 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] | 5663 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5664 | AUDIO_DEVICE_IN_HDMI); |
| 5665 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 5666 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5667 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5668 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5669 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5670 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5671 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5672 | name.c_str(), |
| 5673 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5674 | if (status != NO_ERROR) { |
| 5675 | continue; |
| 5676 | } |
| 5677 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5678 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5679 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5680 | name.c_str(), |
| 5681 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5682 | profileUpdated |= (status == NO_ERROR); |
| 5683 | } |
| 5684 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5685 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5686 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5687 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 5691 | } |
| 5692 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5693 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5694 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5695 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 5696 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5697 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5698 | } |
| 5699 | } |
| 5700 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 5701 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 5702 | { |
| 5703 | for (const auto& hwModule : mHwModules) { |
| 5704 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5705 | for (const auto &outProfile : outputProfiles) { |
| 5706 | struct audio_port audioPort; |
| 5707 | outProfile->toAudioPort(&audioPort); |
| 5708 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 5709 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5710 | return true; |
| 5711 | } |
| 5712 | } |
| 5713 | } |
| 5714 | } |
| 5715 | return false; |
| 5716 | } |
| 5717 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 5718 | bool AudioPolicyManager::isUltrasoundSupported() |
| 5719 | { |
| 5720 | bool hasUltrasoundOutput = false; |
| 5721 | bool hasUltrasoundInput = false; |
| 5722 | for (const auto& hwModule : mHwModules) { |
| 5723 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5724 | if (!hasUltrasoundOutput) { |
| 5725 | for (const auto &outProfile : outputProfiles) { |
| 5726 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 5727 | hasUltrasoundOutput = true; |
| 5728 | break; |
| 5729 | } |
| 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5734 | if (!hasUltrasoundInput) { |
| 5735 | for (const auto &inputProfile : inputProfiles) { |
| 5736 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 5737 | hasUltrasoundInput = true; |
| 5738 | break; |
| 5739 | } |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 5744 | return true; |
| 5745 | } |
| 5746 | return false; |
| 5747 | } |
| 5748 | |
Atneya Nair | 698f5ef | 2022-12-15 16:15:09 -0800 | [diff] [blame] | 5749 | bool AudioPolicyManager::isHotwordStreamSupported(bool lookbackAudio) |
| 5750 | { |
| 5751 | const auto mask = AUDIO_INPUT_FLAG_HOTWORD_TAP | |
| 5752 | (lookbackAudio ? AUDIO_INPUT_FLAG_HW_LOOKBACK : 0); |
| 5753 | for (const auto& hwModule : mHwModules) { |
| 5754 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5755 | for (const auto &inputProfile : inputProfiles) { |
| 5756 | if ((inputProfile->getFlags() & mask) == mask) { |
| 5757 | return true; |
| 5758 | } |
| 5759 | } |
| 5760 | } |
| 5761 | return false; |
| 5762 | } |
| 5763 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5764 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 5765 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5766 | return mConfig->isCallScreenModeSupported(); |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5770 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5771 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5772 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5773 | if (!sourceDesc->isConnected()) { |
| 5774 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 5775 | return NO_ERROR; |
| 5776 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5777 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5778 | if (swOutput != 0) { |
| 5779 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5780 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5781 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5782 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5783 | if (releaseOutput(sourceDesc->portId())) { |
| 5784 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 5785 | // no need to release audio patch here but just return NO_ERROR. |
| 5786 | return NO_ERROR; |
| 5787 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5788 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5789 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5790 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5791 | // close Hwoutput and remove from mHwOutputs |
| 5792 | } else { |
| 5793 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 5794 | } |
| 5795 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5796 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5797 | sourceDesc->disconnect(); |
| 5798 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5799 | } |
| 5800 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5801 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 5802 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5803 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5804 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5805 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5806 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5807 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5808 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 5809 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5810 | source = sourceDesc; |
| 5811 | break; |
| 5812 | } |
| 5813 | } |
| 5814 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5815 | } |
| 5816 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5817 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5818 | const audio_config_t *config, |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5819 | const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5820 | { |
| 5821 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 5822 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5823 | // 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] | 5824 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5825 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 5826 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 5827 | return false; |
| 5828 | } |
| 5829 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 5830 | return false; |
| 5831 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5832 | } |
| 5833 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5834 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5835 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5836 | if (profile == nullptr) { |
| 5837 | return false; |
| 5838 | } |
| 5839 | |
| 5840 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 5841 | // the AUDIO_CONFIG_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5842 | // If an audio config is specified, current policy is to only allow spatialization for |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5843 | // some positional channel masks. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5844 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5845 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5846 | if (!audio_is_channel_mask_spatialized(config->channel_mask)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5847 | return false; |
| 5848 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5849 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5850 | return true; |
| 5851 | } |
| 5852 | |
| 5853 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 5854 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 5855 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5856 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 5857 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5858 | audio_attributes_t attr = client->attributes(); |
| 5859 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 5860 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5861 | audio_config_base_t clientConfig = client->config(); |
| 5862 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5863 | if (desc != mSpatializerOutput |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5864 | && canBeSpatializedInt(&attr, &config, devicesTypeAddress)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5865 | streamsToInvalidate.insert(client->stream()); |
| 5866 | } |
| 5867 | } |
| 5868 | } |
| 5869 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 5870 | invalidateStreams(StreamTypeVector(streamsToInvalidate.begin(), streamsToInvalidate.end())); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5871 | } |
| 5872 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5873 | |
| 5874 | bool AudioPolicyManager::isOutputOnlyAvailableRouteToSomeDevice( |
| 5875 | const sp<SwAudioOutputDescriptor>& outputDesc) { |
| 5876 | if (outputDesc->isDuplicated()) { |
| 5877 | return false; |
| 5878 | } |
| 5879 | DeviceVector devices = outputDesc->supportedDevices(); |
| 5880 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5881 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5882 | if (desc == outputDesc || desc->isDuplicated()) { |
| 5883 | continue; |
| 5884 | } |
| 5885 | DeviceVector sharedDevices = desc->filterSupportedDevices(devices); |
| 5886 | if (!sharedDevices.isEmpty() |
| 5887 | && (desc->devicesSupportEncodedFormats(sharedDevices.types()) |
| 5888 | == outputDesc->devicesSupportEncodedFormats(sharedDevices.types()))) { |
| 5889 | return false; |
| 5890 | } |
| 5891 | } |
| 5892 | return true; |
| 5893 | } |
| 5894 | |
| 5895 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5896 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5897 | const audio_attributes_t *attr, |
| 5898 | audio_io_handle_t *output) { |
| 5899 | *output = AUDIO_IO_HANDLE_NONE; |
| 5900 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5901 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 5902 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5903 | audio_config_t *configPtr = nullptr; |
| 5904 | audio_config_t config; |
| 5905 | if (mixerConfig != nullptr) { |
| 5906 | config = audio_config_initializer(mixerConfig); |
| 5907 | configPtr = &config; |
| 5908 | } |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5909 | if (!canBeSpatializedInt(attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5910 | ALOGV("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5911 | return BAD_VALUE; |
| 5912 | } |
| 5913 | |
| 5914 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5915 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5916 | if (profile == nullptr) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5917 | 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] | 5918 | return BAD_VALUE; |
| 5919 | } |
| 5920 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5921 | std::vector<sp<SwAudioOutputDescriptor>> spatializerOutputs; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5922 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5923 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5924 | if (!desc->isDuplicated() |
| 5925 | && (desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 5926 | spatializerOutputs.push_back(desc); |
| 5927 | ALOGV("%s adding opened spatializer Output %d", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5928 | } |
| 5929 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5930 | mSpatializerOutput.clear(); |
| 5931 | bool outputsChanged = false; |
| 5932 | for (const auto& desc : spatializerOutputs) { |
| 5933 | if (desc->mProfile == profile |
| 5934 | && (configPtr == nullptr |
| 5935 | || configPtr->channel_mask == desc->mMixerChannelMask)) { |
| 5936 | mSpatializerOutput = desc; |
| 5937 | ALOGV("%s reusing current spatializer output %d", __func__, desc->mIoHandle); |
| 5938 | } else { |
| 5939 | ALOGV("%s closing spatializerOutput output %d to match channel mask %#x" |
| 5940 | " and devices %s", __func__, desc->mIoHandle, |
| 5941 | configPtr != nullptr ? configPtr->channel_mask : 0, |
| 5942 | devices.toString().c_str()); |
| 5943 | closeOutput(desc->mIoHandle); |
| 5944 | outputsChanged = true; |
| 5945 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5946 | } |
| 5947 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5948 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5949 | sp<SwAudioOutputDescriptor> desc = |
| 5950 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5951 | if (desc != nullptr) { |
| 5952 | mSpatializerOutput = desc; |
| 5953 | outputsChanged = true; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5954 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | checkVirtualizerClientRoutes(); |
| 5958 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5959 | if (outputsChanged) { |
| 5960 | mPreviousOutputs = mOutputs; |
| 5961 | mpClientInterface->onAudioPortListUpdate(); |
| 5962 | } |
| 5963 | |
| 5964 | if (mSpatializerOutput == nullptr) { |
| 5965 | ALOGV("%s could not open spatializer output with requested config", __func__); |
| 5966 | return BAD_VALUE; |
| 5967 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5968 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5969 | ALOGV("%s returning new spatializer output %d", __func__, *output); |
| 5970 | return OK; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5971 | } |
| 5972 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5973 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 5974 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5975 | return INVALID_OPERATION; |
| 5976 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5977 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5978 | return BAD_VALUE; |
| 5979 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5980 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5981 | if (!isOutputOnlyAvailableRouteToSomeDevice(mSpatializerOutput)) { |
| 5982 | ALOGV("%s closing spatializer output %d", __func__, mSpatializerOutput->mIoHandle); |
| 5983 | closeOutput(mSpatializerOutput->mIoHandle); |
| 5984 | //from now on mSpatializerOutput is null |
| 5985 | checkVirtualizerClientRoutes(); |
| 5986 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5987 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5988 | return NO_ERROR; |
| 5989 | } |
| 5990 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5991 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5992 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5993 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5994 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 5995 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 5996 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5997 | } |
| 5998 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 5999 | AudioPolicyManager::AudioPolicyManager(const sp<const AudioPolicyConfig>& config, |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6000 | EngineInstance&& engine, |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6001 | AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6002 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 6003 | 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] | 6004 | mConfig(config), |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6005 | mEngine(std::move(engine)), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6006 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6007 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6008 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6009 | mAudioPortGeneration(1), |
| 6010 | mBeaconMuteRefCount(0), |
| 6011 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 6012 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6013 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6014 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 6015 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6016 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6017 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6018 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6019 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | f1b6d97 | 2023-05-02 13:56:01 -0700 | [diff] [blame] | 6020 | if (mEngine == nullptr) { |
| 6021 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6022 | } |
| 6023 | mEngine->setObserver(this); |
| 6024 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6025 | if (status != NO_ERROR) { |
| 6026 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 6027 | return status; |
| 6028 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6029 | |
jiabin | 2923018 | 2023-04-04 21:02:36 +0000 | [diff] [blame] | 6030 | // The actual device selection cache will be updated when calling `updateDevicesAndOutputs` |
| 6031 | // at the end of this function. |
| 6032 | mEngine->initializeDeviceSelectionCache(); |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 6033 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 6034 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 6035 | |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6036 | // after parsing the config, mConfig contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6037 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6038 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6039 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6040 | // make sure default device is reachable |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6041 | if (const auto defaultOutputDevice = mConfig->getDefaultOutputDevice(); |
| 6042 | defaultOutputDevice == nullptr || |
| 6043 | !mAvailableOutputDevices.contains(defaultOutputDevice)) { |
| 6044 | ALOGE_IF(defaultOutputDevice != nullptr, "Default device %s is unreachable", |
| 6045 | defaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6046 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6047 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6048 | 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] | 6049 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 6050 | // Silence ALOGV statements |
| 6051 | property_set("log.tag." LOG_TAG, "D"); |
| 6052 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6053 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 6054 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6055 | } |
| 6056 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6057 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6058 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6059 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6060 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6061 | } |
| 6062 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6063 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6064 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6065 | mAvailableOutputDevices.clear(); |
| 6066 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6067 | mOutputs.clear(); |
| 6068 | mInputs.clear(); |
| 6069 | mHwModules.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6070 | mManualSurroundFormats.clear(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6071 | mConfig.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6072 | } |
| 6073 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6074 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6075 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 6076 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6077 | } |
| 6078 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6079 | // --- |
| 6080 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6081 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 6082 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6083 | DeviceVector newDevices; |
| 6084 | onNewAudioModulesAvailableInt(&newDevices); |
| 6085 | if (!newDevices.empty()) { |
| 6086 | nextAudioPortGeneration(); |
| 6087 | mpClientInterface->onAudioPortListUpdate(); |
| 6088 | } |
| 6089 | } |
| 6090 | |
| 6091 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 6092 | { |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6093 | for (const auto& hwModule : mConfig->getHwModules()) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6094 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 6095 | continue; |
| 6096 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6097 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame^] | 6098 | if (audio_module_handle_t handle = mpClientInterface->loadHwModule(hwModule->getName()); |
| 6099 | handle != AUDIO_MODULE_HANDLE_NONE) { |
| 6100 | hwModule->setHandle(handle); |
| 6101 | } else { |
| 6102 | ALOGW("could not load HW module %s", hwModule->getName()); |
| 6103 | continue; |
| 6104 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6105 | } |
| 6106 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 6107 | // open all output streams needed to access attached devices. |
| 6108 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6109 | // This also validates mAvailableOutputDevices list |
| 6110 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 6111 | if (!outProfile->canOpenNewIo()) { |
| 6112 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 6113 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 6114 | continue; |
| 6115 | } |
| 6116 | if (!outProfile->hasSupportedDevices()) { |
| 6117 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 6118 | continue; |
| 6119 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 6120 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 6121 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6122 | mTtsOutputAvailable = true; |
| 6123 | } |
| 6124 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6125 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6126 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getOutputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6127 | sp<DeviceDescriptor> supportedDevice = 0; |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6128 | if (supportedDevices.contains(mConfig->getDefaultOutputDevice())) { |
| 6129 | supportedDevice = mConfig->getDefaultOutputDevice(); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6130 | } else { |
| 6131 | // choose first device present in profile's SupportedDevices also part of |
| 6132 | // mAvailableOutputDevices. |
| 6133 | if (availProfileDevices.isEmpty()) { |
| 6134 | continue; |
| 6135 | } |
| 6136 | supportedDevice = availProfileDevices.itemAt(0); |
| 6137 | } |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6138 | if (!mConfig->getOutputDevices().contains(supportedDevice)) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6139 | continue; |
| 6140 | } |
| 6141 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 6142 | mpClientInterface); |
| 6143 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 6144 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 6145 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6146 | AUDIO_STREAM_DEFAULT, |
| 6147 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 6148 | if (status != NO_ERROR) { |
| 6149 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 6150 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 6151 | continue; |
| 6152 | } |
| 6153 | for (const auto &device : availProfileDevices) { |
| 6154 | // give a valid ID to an attached device once confirmed it is reachable |
| 6155 | if (!device->isAttached()) { |
| 6156 | device->attach(hwModule); |
| 6157 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 6158 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6159 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6160 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6161 | } |
| 6162 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6163 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6164 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 6165 | mPrimaryOutput = outputDesc; |
| 6166 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6167 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6168 | outputDesc->close(); |
| 6169 | } else { |
| 6170 | addOutput(output, outputDesc); |
| 6171 | setOutputDevices(outputDesc, |
| 6172 | DeviceVector(supportedDevice), |
| 6173 | true, |
| 6174 | 0, |
| 6175 | NULL); |
| 6176 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6177 | } |
| 6178 | // open input streams needed to access attached devices to validate |
| 6179 | // mAvailableInputDevices list |
| 6180 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 6181 | if (!inProfile->canOpenNewIo()) { |
| 6182 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 6183 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 6184 | continue; |
| 6185 | } |
| 6186 | if (!inProfile->hasSupportedDevices()) { |
| 6187 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 6188 | continue; |
| 6189 | } |
| 6190 | // chose first device present in profile's SupportedDevices also part of |
| 6191 | // available input devices |
| 6192 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 6193 | DeviceVector availProfileDevices = supportedDevices.filter(mConfig->getInputDevices()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6194 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6195 | ALOGV("%s: Input device list is empty! for profile %s", |
| 6196 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6197 | continue; |
| 6198 | } |
| 6199 | sp<AudioInputDescriptor> inputDesc = |
| 6200 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 6201 | |
| 6202 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 6203 | status_t status = inputDesc->open(nullptr, |
| 6204 | availProfileDevices.itemAt(0), |
| 6205 | AUDIO_SOURCE_MIC, |
| 6206 | AUDIO_INPUT_FLAG_NONE, |
| 6207 | &input); |
| 6208 | if (status != NO_ERROR) { |
| 6209 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 6210 | availProfileDevices.toString().c_str(), |
| 6211 | hwModule->getName()); |
| 6212 | continue; |
| 6213 | } |
| 6214 | for (const auto &device : availProfileDevices) { |
| 6215 | // give a valid ID to an attached device once confirmed it is reachable |
| 6216 | if (!device->isAttached()) { |
| 6217 | device->attach(hwModule); |
| 6218 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 6219 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6220 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6221 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6222 | } |
| 6223 | } |
| 6224 | inputDesc->close(); |
| 6225 | } |
| 6226 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6227 | |
| 6228 | // Check if spatializer outputs can be closed until used. |
| 6229 | // mOutputs vector never contains duplicated outputs at this point. |
| 6230 | std::vector<audio_io_handle_t> outputsClosed; |
| 6231 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6232 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6233 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0 |
| 6234 | && !isOutputOnlyAvailableRouteToSomeDevice(desc)) { |
| 6235 | outputsClosed.push_back(desc->mIoHandle); |
| 6236 | desc->close(); |
| 6237 | } |
| 6238 | } |
| 6239 | for (auto output : outputsClosed) { |
| 6240 | removeOutput(output); |
| 6241 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6242 | } |
| 6243 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6244 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 6245 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6246 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6247 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6248 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6249 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6250 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6251 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6252 | } |
| 6253 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6254 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 6255 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6256 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 6257 | ALOGV("%s: removing primary output", __func__); |
| 6258 | mPrimaryOutput = nullptr; |
| 6259 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6260 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6261 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6262 | } |
| 6263 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6264 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 6265 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6266 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6267 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6268 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6269 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6270 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6271 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6272 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6273 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6274 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6275 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6276 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6277 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6278 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6279 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6280 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6281 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6282 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6283 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6284 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 6285 | // first call getAudioPort to get the supported attributes from the HAL |
| 6286 | struct audio_port_v7 port = {}; |
| 6287 | device->toAudioPort(&port); |
| 6288 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6289 | if (status == NO_ERROR) { |
| 6290 | device->importAudioPort(port); |
| 6291 | } |
| 6292 | |
| 6293 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6294 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6295 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6296 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6297 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6298 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 6299 | mOutputs.keyAt(i), device->toString().c_str()); |
| 6300 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6301 | } |
| 6302 | } |
| 6303 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6304 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6305 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6306 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6307 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6308 | if (profile->supportsDevice(device)) { |
| 6309 | profiles.add(profile); |
| 6310 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 6311 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6312 | } |
| 6313 | } |
| 6314 | } |
| 6315 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 6316 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6317 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6318 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6319 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6320 | return BAD_VALUE; |
| 6321 | } |
| 6322 | |
| 6323 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 6324 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6325 | 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] | 6326 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6327 | |
| 6328 | // nothing to do if one output is already opened for this profile |
| 6329 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6330 | for (j = 0; j < outputs.size(); j++) { |
| 6331 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6332 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6333 | // matching profile: save the sample rates, format and channel masks supported |
| 6334 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6335 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6336 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6337 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6338 | break; |
| 6339 | } |
| 6340 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6341 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6342 | continue; |
| 6343 | } |
| 6344 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6345 | if (!profile->canOpenNewIo()) { |
| 6346 | ALOGW("Max Output number %u already opened for this profile %s", |
| 6347 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6348 | continue; |
| 6349 | } |
| 6350 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 6351 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 6352 | deviceType, address.string(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6353 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 6354 | 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] | 6355 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6356 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6357 | profiles.removeAt(profile_index); |
| 6358 | profile_index--; |
| 6359 | } else { |
| 6360 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6361 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6362 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6363 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 6364 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6365 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6366 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6367 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6368 | if (device_distinguishes_on_address(deviceType)) { |
| 6369 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 6370 | device->toString().c_str()); |
| 6371 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 6372 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6373 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6374 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 6375 | } |
| 6376 | } |
| 6377 | |
| 6378 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6379 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6380 | return BAD_VALUE; |
| 6381 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6382 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6383 | // check if one opened output is not needed any more after disconnecting one device |
| 6384 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6385 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6386 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6387 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6388 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 6389 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6390 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6391 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6392 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 6393 | mOutputs.keyAt(i)); |
| 6394 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6395 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6396 | } |
| 6397 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6398 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6399 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6400 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6401 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6402 | if (!profile->supportsDevice(device)) { |
| 6403 | continue; |
| 6404 | } |
| 6405 | ALOGV("checkOutputsForDevice(): " |
| 6406 | "clearing direct output profile %zu on module %s", |
| 6407 | j, hwModule->getName()); |
| 6408 | profile->clearAudioProfiles(); |
| 6409 | if (!profile->hasDynamicAudioProfile()) { |
| 6410 | continue; |
| 6411 | } |
| 6412 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 6413 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 6414 | // the capabilities of the devices that is supported by the IOProfile. |
| 6415 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 6416 | if (supportedDevice == device || |
| 6417 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 6418 | continue; |
| 6419 | } |
| 6420 | struct audio_port_v7 port; |
| 6421 | supportedDevice->toAudioPort(&port); |
| 6422 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6423 | if (status == NO_ERROR) { |
| 6424 | supportedDevice->importAudioPort(port); |
| 6425 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6426 | } |
| 6427 | } |
| 6428 | } |
| 6429 | } |
| 6430 | return NO_ERROR; |
| 6431 | } |
| 6432 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6433 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6434 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6435 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6436 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6437 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6438 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6439 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6440 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6441 | } |
| 6442 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6443 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6444 | // first call getAudioPort to get the supported attributes from the HAL |
| 6445 | struct audio_port_v7 port = {}; |
| 6446 | device->toAudioPort(&port); |
| 6447 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6448 | if (status == NO_ERROR) { |
| 6449 | device->importAudioPort(port); |
| 6450 | } |
| 6451 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6452 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6453 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6454 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6455 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6456 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6457 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6458 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6459 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6460 | if (profile->supportsDevice(device)) { |
| 6461 | profiles.add(profile); |
| 6462 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 6463 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6464 | } |
| 6465 | } |
| 6466 | } |
| 6467 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6468 | if (profiles.isEmpty()) { |
| 6469 | ALOGW("%s: No input profile available for device %s", |
| 6470 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6471 | return BAD_VALUE; |
| 6472 | } |
| 6473 | |
| 6474 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 6475 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6476 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 6477 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6478 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6479 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6480 | // nothing to do if one input is already opened for this profile |
| 6481 | size_t input_index; |
| 6482 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 6483 | desc = mInputs.valueAt(input_index); |
| 6484 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6485 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6486 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6487 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6488 | break; |
| 6489 | } |
| 6490 | } |
| 6491 | if (input_index != mInputs.size()) { |
| 6492 | continue; |
| 6493 | } |
| 6494 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6495 | if (!profile->canOpenNewIo()) { |
| 6496 | ALOGW("Max Input number %u already opened for this profile %s", |
| 6497 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6498 | continue; |
| 6499 | } |
| 6500 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6501 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6502 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
jiabin | bf5f426 | 2023-04-12 21:48:34 +0000 | [diff] [blame] | 6503 | status = desc->open(nullptr, device, AUDIO_SOURCE_MIC, AUDIO_INPUT_FLAG_NONE, &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6504 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6505 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6506 | const String8& address = String8(device->address().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6507 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6508 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6509 | mpClientInterface->setParameters(input, String8(param)); |
| 6510 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6511 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6512 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6513 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6514 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6515 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6516 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6517 | } |
| 6518 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6519 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6520 | addInput(input, desc); |
| 6521 | } |
| 6522 | } // endif input != 0 |
| 6523 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6524 | if (input == AUDIO_IO_HANDLE_NONE) { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 6525 | ALOGW("%s could not open input for device %s", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6526 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6527 | profiles.removeAt(profile_index); |
| 6528 | profile_index--; |
| 6529 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6530 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6531 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6532 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6533 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 6534 | } |
| 6535 | } // end scan profiles |
| 6536 | |
| 6537 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6538 | 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] | 6539 | return BAD_VALUE; |
| 6540 | } |
| 6541 | } else { |
| 6542 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6543 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6544 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6545 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6546 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6547 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6548 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6549 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6550 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 6551 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6552 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6553 | } |
| 6554 | } |
| 6555 | } |
| 6556 | } // end disconnect |
| 6557 | |
| 6558 | return NO_ERROR; |
| 6559 | } |
| 6560 | |
| 6561 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6562 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6563 | { |
| 6564 | ALOGV("closeOutput(%d)", output); |
| 6565 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6566 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 6567 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6568 | ALOGW("closeOutput() unknown output %d", output); |
| 6569 | return; |
| 6570 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6571 | const bool closingOutputWasActive = closingOutput->isActive(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6572 | mPolicyMixes.closeOutput(closingOutput); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6573 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6574 | // look for duplicated outputs connected to the output being removed. |
| 6575 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6576 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 6577 | if (dupOutput->isDuplicated() && |
| 6578 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 6579 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 6580 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6581 | // As all active tracks on duplicated output will be deleted, |
| 6582 | // and as they were also referenced on the other output, the reference |
| 6583 | // count for their stream type must be adjusted accordingly on |
| 6584 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6585 | const bool wasActive = remainingOutput->isActive(); |
| 6586 | // Note: no-op on the closing output where all clients has already been set inactive |
| 6587 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6588 | // stop() will be a no op if the output is still active but is needed in case all |
| 6589 | // active streams refcounts where cleared above |
| 6590 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6591 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6592 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6593 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 6594 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 6595 | |
| 6596 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6597 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6598 | } |
| 6599 | } |
| 6600 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6601 | nextAudioPortGeneration(); |
| 6602 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6603 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6604 | if (index >= 0) { |
| 6605 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6606 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6607 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6608 | mAudioPatches.removeItemsAt(index); |
| 6609 | mpClientInterface->onAudioPatchListUpdate(); |
| 6610 | } |
| 6611 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6612 | if (closingOutputWasActive) { |
| 6613 | closingOutput->stop(); |
| 6614 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6615 | closingOutput->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6616 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6617 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6618 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6619 | if (closingOutput == mSpatializerOutput) { |
| 6620 | mSpatializerOutput.clear(); |
| 6621 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6622 | |
| 6623 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 6624 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6625 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6626 | bool directOutputOpen = false; |
| 6627 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6628 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 6629 | directOutputOpen = true; |
| 6630 | break; |
| 6631 | } |
| 6632 | } |
| 6633 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6634 | ALOGV("no direct outputs open, reset MSD patches"); |
| 6635 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 6636 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 6637 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 6638 | // devices to patch to. This may be complicated by the fact that devices may become |
| 6639 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6640 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6641 | } |
| 6642 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6643 | } |
| 6644 | |
| 6645 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 6646 | { |
| 6647 | ALOGV("closeInput(%d)", input); |
| 6648 | |
| 6649 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 6650 | if (inputDesc == NULL) { |
| 6651 | ALOGW("closeInput() unknown input %d", input); |
| 6652 | return; |
| 6653 | } |
| 6654 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6655 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6656 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6657 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6658 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6659 | if (index >= 0) { |
| 6660 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6661 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6662 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6663 | mAudioPatches.removeItemsAt(index); |
| 6664 | mpClientInterface->onAudioPatchListUpdate(); |
| 6665 | } |
| 6666 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6667 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6668 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6669 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6670 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 6671 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6672 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 6673 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6674 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6675 | } |
| 6676 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6677 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 6678 | const DeviceVector &devices, |
| 6679 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6680 | { |
| 6681 | SortedVector<audio_io_handle_t> outputs; |
| 6682 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6683 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6684 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6685 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 6686 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6687 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6688 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6689 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6690 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6691 | outputs.add(openOutputs.keyAt(i)); |
| 6692 | } |
| 6693 | } |
| 6694 | return outputs; |
| 6695 | } |
| 6696 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6697 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 6698 | { |
| 6699 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 6700 | // output is suspended before any tracks are moved to it |
| 6701 | checkA2dpSuspend(); |
| 6702 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6703 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6704 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6705 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 6706 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6707 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 6708 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 6709 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 6710 | // unnecessary rerouting by caching and reusing the arguments to |
| 6711 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 6712 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6713 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6714 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 6715 | // an event that changed routing likely occurred, inform upper layers |
| 6716 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6717 | } |
| 6718 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6719 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 6720 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6721 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6722 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 6723 | mEngine->getProductStrategyForAttributes(rAttr); |
| 6724 | } |
| 6725 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6726 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 6727 | { |
| 6728 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6729 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6730 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6731 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6732 | && !isCallRxAudioSource(sourceDesc) && !sourceDesc->isInternal()) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6733 | connectAudioSource(sourceDesc); |
| 6734 | } |
| 6735 | } |
| 6736 | } |
| 6737 | |
| 6738 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 6739 | { |
| 6740 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6741 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6742 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 6743 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 6744 | disconnectAudioSource(sourceDesc); |
| 6745 | } |
| 6746 | } |
| 6747 | } |
| 6748 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6749 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 6750 | { |
| 6751 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 6752 | |
| 6753 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 6754 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 6755 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6756 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 6757 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6758 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6759 | uint32_t maxLatency = 0; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6760 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6761 | std::vector<sp<SwAudioOutputDescriptor>> invalidatedOutputs; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6762 | // take into account dynamic audio policies related changes: if a client is now associated |
| 6763 | // to a different policy mix than at creation time, invalidate corresponding stream |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6764 | for (size_t i = 0; i < mPreviousOutputs.size(); i++) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6765 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 6766 | if (desc->isDuplicated()) { |
| 6767 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6768 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6769 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 6770 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 6771 | continue; |
| 6772 | } |
| 6773 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6774 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6775 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 6776 | nullptr /* requestedDevice */, primaryMix, nullptr /* secondaryMixes */, |
| 6777 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6778 | if (status != OK) { |
| 6779 | continue; |
| 6780 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 6781 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6782 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6783 | maxLatency = desc->latency(); |
| 6784 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6785 | invalidatedOutputs.push_back(desc); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6786 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6787 | } |
| 6788 | } |
| 6789 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6790 | if (srcOutputs != dstOutputs || !invalidatedOutputs.empty()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6791 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 6792 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6793 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6794 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6795 | if (desc == nullptr) continue; |
| 6796 | |
| 6797 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6798 | maxLatency = desc->latency(); |
| 6799 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6800 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6801 | bool invalidate = false; |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6802 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 6803 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6804 | // a client on a non direct outputs has necessarily a linear PCM format |
| 6805 | // so we can call selectOutput() safely |
| 6806 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 6807 | client->flags(), |
| 6808 | client->config().format, |
| 6809 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 6810 | client->config().sample_rate, |
| 6811 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6812 | if (newOutput != srcOut) { |
| 6813 | invalidate = true; |
| 6814 | break; |
| 6815 | } |
| 6816 | } else { |
| 6817 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 6818 | client->config().sample_rate, |
| 6819 | client->config().format, |
| 6820 | client->config().channel_mask, |
| 6821 | client->flags(), |
| 6822 | true /* directOnly */); |
| 6823 | if (profile != desc->mProfile) { |
| 6824 | invalidate = true; |
| 6825 | break; |
| 6826 | } |
| 6827 | } |
| 6828 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6829 | // mute strategy while moving tracks from one output to another |
| 6830 | if (invalidate) { |
| 6831 | invalidatedOutputs.push_back(desc); |
| 6832 | if (desc->isStrategyActive(psId)) { |
| 6833 | setStrategyMute(psId, true, desc); |
| 6834 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 6835 | newDevices.types()); |
| 6836 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6837 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6838 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6839 | if (source != nullptr && !isCallRxAudioSource(source) && !source->isInternal()) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6840 | connectAudioSource(source); |
| 6841 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6842 | } |
| 6843 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6844 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
| 6845 | "%s: strategy %d, moving from output %s to output %s", __func__, psId, |
| 6846 | std::to_string(srcOutputs[0]).c_str(), |
| 6847 | std::to_string(dstOutputs[0]).c_str()); |
| 6848 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6849 | // Move effects associated to this stream from previous output to new output |
| 6850 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6851 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6852 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6853 | // 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] | 6854 | if (!invalidatedOutputs.empty()) { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6855 | invalidateStreams(mEngine->getStreamTypesForProductStrategy(psId)); |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6856 | for (sp<SwAudioOutputDescriptor> desc : invalidatedOutputs) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 6857 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 6858 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6859 | } |
| 6860 | } |
| 6861 | } |
| 6862 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6863 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6864 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6865 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 6866 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 6867 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6868 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6869 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6870 | } |
| 6871 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6872 | void AudioPolicyManager::checkSecondaryOutputs() { |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6873 | PortHandleVector clientsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6874 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6875 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6876 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6877 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 6878 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6879 | sp<AudioPolicyMix> primaryMix; |
| 6880 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6881 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 6882 | client->uid(), client->session(), client->flags(), mAvailableOutputDevices, |
| 6883 | nullptr /* requestedDevice */, primaryMix, &secondaryMixes, |
| 6884 | unneededUsePrimaryOutputFromPolicyMixes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6885 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 6886 | for (auto &secondaryMix : secondaryMixes) { |
| 6887 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 6888 | if (outputDesc != nullptr && |
| 6889 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 6890 | secondaryDescs.push_back(outputDesc); |
| 6891 | } |
| 6892 | } |
| 6893 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6894 | if (status != OK && |
| 6895 | (client->flags() & AUDIO_OUTPUT_FLAG_MMAP_NOIRQ) == AUDIO_OUTPUT_FLAG_NONE) { |
| 6896 | // When it failed to query secondary output, only invalidate the client that is not |
| 6897 | // MMAP. The reason is that MMAP stream will not support secondary output. |
| 6898 | clientsToInvalidate.push_back(client->portId()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6899 | } else if (!std::equal( |
| 6900 | client->getSecondaryOutputs().begin(), |
| 6901 | client->getSecondaryOutputs().end(), |
| 6902 | secondaryDescs.begin(), secondaryDescs.end())) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6903 | if (!audio_is_linear_pcm(client->config().format)) { |
| 6904 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 6905 | // behavior when the secondary output is changed. |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6906 | clientsToInvalidate.push_back(client->portId()); |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6907 | } else { |
| 6908 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 6909 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 6910 | for (const auto &secondaryDesc: secondaryDescs) { |
| 6911 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 6912 | weakSecondaryDescs.push_back(secondaryDesc); |
| 6913 | } |
| 6914 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 6915 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6916 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6917 | } |
| 6918 | } |
| 6919 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6920 | if (!trackSecondaryOutputs.empty()) { |
| 6921 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 6922 | } |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 6923 | if (!clientsToInvalidate.empty()) { |
| 6924 | ALOGD("%s Invalidate clients due to fail getting output for attr", __func__); |
| 6925 | mpClientInterface->invalidateTracks(clientsToInvalidate); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6926 | } |
| 6927 | } |
| 6928 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6929 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 6930 | AudioDeviceTypeAddrVector devices; |
| 6931 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 6932 | for (const auto &device : devices) { |
| 6933 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 6934 | return true; |
| 6935 | } |
| 6936 | } |
| 6937 | return false; |
| 6938 | } |
| 6939 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 6940 | bool AudioPolicyManager::isHearingAidUsedForComm() const { |
| 6941 | DeviceVector devices = mEngine->getOutputDevicesForStream(AUDIO_STREAM_VOICE_CALL, |
| 6942 | true /*fromCache*/); |
| 6943 | for (const auto &device : devices) { |
| 6944 | if (device->type() == AUDIO_DEVICE_OUT_HEARING_AID) { |
| 6945 | return true; |
| 6946 | } |
| 6947 | } |
| 6948 | return false; |
| 6949 | } |
| 6950 | |
| 6951 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6952 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6953 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6954 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 6955 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6956 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6957 | return; |
| 6958 | } |
| 6959 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6960 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6961 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 6962 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6963 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6964 | |
| 6965 | // if suspended, restore A2DP output if: |
| 6966 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6967 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6968 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6969 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6970 | // if not suspended, suspend A2DP output if: |
| 6971 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6972 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6973 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6974 | // |
| 6975 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6976 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6977 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6978 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6979 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6980 | |
| 6981 | mpClientInterface->restoreOutput(a2dpOutput); |
| 6982 | mA2dpSuspended = false; |
| 6983 | } |
| 6984 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6985 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6986 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6987 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6988 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6989 | |
| 6990 | mpClientInterface->suspendOutput(a2dpOutput); |
| 6991 | mA2dpSuspended = true; |
| 6992 | } |
| 6993 | } |
| 6994 | } |
| 6995 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6996 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 6997 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6998 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6999 | DeviceVector devices; |
| 7000 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7001 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7002 | if (index >= 0) { |
| 7003 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7004 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7005 | ALOGV("%s device %s forced by patch %d", __func__, |
| 7006 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 7007 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7008 | } |
| 7009 | } |
| 7010 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 7011 | // Do not retrieve engine device for outputs through MSD |
| 7012 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 7013 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 7014 | return outputDesc->devices(); |
| 7015 | } |
| 7016 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7017 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7018 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7019 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7020 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7021 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7022 | if (device != nullptr) { |
| 7023 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 7024 | } |
| 7025 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 7026 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 7027 | // of setForceUse / Default Bus device here |
| 7028 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 7029 | if (device != nullptr) { |
| 7030 | return DeviceVector(device); |
| 7031 | } |
| 7032 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7033 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 7034 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
| 7035 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7036 | auto hasStreamActive = [&](auto stream) { |
| 7037 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 7038 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 7039 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7040 | auto doGetOutputDevicesForVoice = [&]() { |
| 7041 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7042 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7043 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 7044 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 7045 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 7046 | }; |
| 7047 | |
| 7048 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 7049 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 7050 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 7051 | // 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] | 7052 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 7053 | // stream is associated to the output descriptor. |
| 7054 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 7055 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 7056 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 7057 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7058 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 7059 | // check the route (would force modifying configuration file for this profile) |
| 7060 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 7061 | break; |
| 7062 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7063 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7064 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7065 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7066 | } |
| 7067 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7068 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 7069 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7070 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7071 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7072 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7073 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7074 | if (index >= 0) { |
| 7075 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7076 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7077 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 7078 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 7079 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7080 | } |
| 7081 | } |
| 7082 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7083 | // Honor explicit routing requests only if no client using default routing is active on this |
| 7084 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 7085 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7086 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 7087 | if (device != nullptr) { |
| 7088 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 7089 | } |
| 7090 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7091 | // 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] | 7092 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7093 | audio_attributes_t attributes; |
| 7094 | uid_t uid; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7095 | audio_session_t session; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7096 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 7097 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7098 | attributes = topClient->attributes(); |
| 7099 | uid = topClient->uid(); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7100 | session = topClient->session(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7101 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7102 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 7103 | uid = 0; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7104 | session = AUDIO_SESSION_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7105 | } |
| 7106 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7107 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 7108 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7109 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7110 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7111 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 7112 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7113 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7114 | return device; |
| 7115 | } |
| 7116 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 7117 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 7118 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 7119 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 7120 | } |
| 7121 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7122 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7123 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7124 | if (devices == nullptr) { |
| 7125 | return BAD_VALUE; |
| 7126 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7127 | |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7128 | DeviceVector curDevices; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 7129 | if (status_t status = getDevicesForAttributes(attr, curDevices, forVolume); status != OK) { |
| 7130 | return status; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7131 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7132 | for (const auto& device : curDevices) { |
| 7133 | devices->push_back(device->getDeviceTypeAddr()); |
| 7134 | } |
| 7135 | return NO_ERROR; |
| 7136 | } |
| 7137 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7138 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7139 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7140 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7141 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7142 | updateDevicesAndOutputs(); |
| 7143 | break; |
| 7144 | default: |
| 7145 | break; |
| 7146 | } |
| 7147 | } |
| 7148 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7149 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 7150 | |
| 7151 | // skip beacon mute management if a dedicated TTS output is available |
| 7152 | if (mTtsOutputAvailable) { |
| 7153 | return 0; |
| 7154 | } |
| 7155 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7156 | switch(event) { |
| 7157 | case STARTING_OUTPUT: |
| 7158 | mBeaconMuteRefCount++; |
| 7159 | break; |
| 7160 | case STOPPING_OUTPUT: |
| 7161 | if (mBeaconMuteRefCount > 0) { |
| 7162 | mBeaconMuteRefCount--; |
| 7163 | } |
| 7164 | break; |
| 7165 | case STARTING_BEACON: |
| 7166 | mBeaconPlayingRefCount++; |
| 7167 | break; |
| 7168 | case STOPPING_BEACON: |
| 7169 | if (mBeaconPlayingRefCount > 0) { |
| 7170 | mBeaconPlayingRefCount--; |
| 7171 | } |
| 7172 | break; |
| 7173 | } |
| 7174 | |
| 7175 | if (mBeaconMuteRefCount > 0) { |
| 7176 | // any playback causes beacon to be muted |
| 7177 | return setBeaconMute(true); |
| 7178 | } else { |
| 7179 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 7180 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 7181 | } |
| 7182 | } |
| 7183 | |
| 7184 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 7185 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 7186 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 7187 | // keep track of muted state to avoid repeating mute/unmute operations |
| 7188 | if (mBeaconMuted != mute) { |
| 7189 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 7190 | ALOGV("\t muting %d", mute); |
| 7191 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7192 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 7193 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 7194 | ALOGV("\t no tts volume source available"); |
| 7195 | return 0; |
| 7196 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7197 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7198 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7199 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7200 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 7201 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7202 | maxLatency = latency; |
| 7203 | } |
| 7204 | } |
| 7205 | mBeaconMuted = mute; |
| 7206 | return maxLatency; |
| 7207 | } |
| 7208 | return 0; |
| 7209 | } |
| 7210 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7211 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7212 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7213 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7214 | mPreviousOutputs = mOutputs; |
| 7215 | } |
| 7216 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 7217 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7218 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7219 | uint32_t delayMs) |
| 7220 | { |
| 7221 | // mute/unmute strategies using an incompatible device combination |
| 7222 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 7223 | // if unmuting, unmute only after the specified delay |
| 7224 | if (outputDesc->isDuplicated()) { |
| 7225 | return 0; |
| 7226 | } |
| 7227 | |
| 7228 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7229 | DeviceVector devices = outputDesc->devices(); |
| 7230 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7231 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7232 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 7233 | for (const auto &productStrategy : productStrategies) { |
| 7234 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 7235 | DeviceVector curDevices = |
| 7236 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 7237 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 7238 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7239 | bool doMute = false; |
| 7240 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7241 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7242 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7243 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 7244 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7245 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7246 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7247 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7248 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7249 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7250 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7251 | // 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] | 7252 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7253 | continue; |
| 7254 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7255 | ALOGVV("%s() %s (curDevice %s)", __func__, |
| 7256 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 7257 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 7258 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7259 | if (mute) { |
| 7260 | // FIXME: should not need to double latency if volume could be applied |
| 7261 | // immediately by the audioflinger mixer. We must account for the delay |
| 7262 | // between now and the next time the audioflinger thread for this output |
| 7263 | // will process a buffer (which corresponds to one buffer size, |
| 7264 | // usually 1/2 or 1/4 of the latency). |
| 7265 | if (muteWaitMs < desc->latency() * 2) { |
| 7266 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7267 | } |
| 7268 | } |
| 7269 | } |
| 7270 | } |
| 7271 | } |
| 7272 | } |
| 7273 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7274 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 7275 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7276 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7277 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7278 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7279 | if (muteWaitMs < tempMuteWaitMs) { |
| 7280 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7281 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7282 | |
| 7283 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 7284 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 7285 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 7286 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 7287 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 7288 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 7289 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7290 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 7291 | // make sure that we do not start the temporary mute period too early in case of |
| 7292 | // delayed device change |
| 7293 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 7294 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7295 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7296 | } |
| 7297 | } |
| 7298 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7299 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 7300 | if (muteWaitMs > delayMs) { |
| 7301 | muteWaitMs -= delayMs; |
| 7302 | usleep(muteWaitMs * 1000); |
| 7303 | return muteWaitMs; |
| 7304 | } |
| 7305 | return 0; |
| 7306 | } |
| 7307 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7308 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7309 | const DeviceVector &devices, |
| 7310 | bool force, |
| 7311 | int delayMs, |
| 7312 | audio_patch_handle_t *patchHandle, |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7313 | bool requiresMuteCheck, bool requiresVolumeCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7314 | { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 7315 | // TODO(b/262404095): Consider if the output need to be reopened. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7316 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7317 | uint32_t muteWaitMs; |
| 7318 | |
| 7319 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7320 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 7321 | nullptr /* patchHandle */, requiresMuteCheck); |
| 7322 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 7323 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7324 | return muteWaitMs; |
| 7325 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7326 | |
| 7327 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7328 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7329 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7330 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7331 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7332 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 7333 | |
| 7334 | if (!filteredDevices.isEmpty()) { |
| 7335 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7336 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7337 | |
| 7338 | // if the outputs are not materially active, there is no need to mute. |
| 7339 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7340 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7341 | } else { |
| 7342 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 7343 | muteWaitMs = 0; |
| 7344 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7345 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7346 | bool outputRouted = outputDesc->isRouted(); |
| 7347 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7348 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 7349 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 7350 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7351 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7352 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 7353 | // restore previous device after evaluating strategy mute state |
| 7354 | outputDesc->setDevices(prevDevices); |
| 7355 | return muteWaitMs; |
| 7356 | } |
| 7357 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7358 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 7359 | // the requested device is AUDIO_DEVICE_NONE |
| 7360 | // OR the requested device is the same as current device |
| 7361 | // AND force is not specified |
| 7362 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7363 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7364 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7365 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 7366 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7367 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
| 7368 | ALOGV("%s setting same device on routed output, force apply volumes", __func__); |
| 7369 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 7370 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7371 | return muteWaitMs; |
| 7372 | } |
| 7373 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7374 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7375 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7376 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7377 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7378 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7379 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7380 | PatchBuilder patchBuilder; |
| 7381 | patchBuilder.addSource(outputDesc); |
| 7382 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 7383 | for (const auto &filteredDevice : filteredDevices) { |
| 7384 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 7385 | } |
| 7386 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 7387 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 7388 | // to avoid disordered sequence of muting volume and changing devices. |
| 7389 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), |
| 7390 | muteWaitMs == 0 ? (delayMs + (outputDesc->latency() / 2)) : delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7391 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7392 | |
| 7393 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7394 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7395 | |
| 7396 | return muteWaitMs; |
| 7397 | } |
| 7398 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7399 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7400 | int delayMs, |
| 7401 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7402 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7403 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7404 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 7405 | return INVALID_OPERATION; |
| 7406 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7407 | if (patchHandle) { |
| 7408 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7409 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7410 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7411 | } |
| 7412 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7413 | return INVALID_OPERATION; |
| 7414 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7415 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7416 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7417 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7418 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7419 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7420 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7421 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7422 | return status; |
| 7423 | } |
| 7424 | |
| 7425 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7426 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7427 | bool force, |
| 7428 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7429 | { |
| 7430 | status_t status = NO_ERROR; |
| 7431 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7432 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7433 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 7434 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7435 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7436 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7437 | PatchBuilder patchBuilder; |
| 7438 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 7439 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 7440 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7441 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 7442 | auto result = usecase; |
| 7443 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 7444 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 7445 | } |
| 7446 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7447 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7448 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7449 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7450 | } |
| 7451 | } |
| 7452 | return status; |
| 7453 | } |
| 7454 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7455 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 7456 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7457 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7458 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7459 | ssize_t index; |
| 7460 | if (patchHandle) { |
| 7461 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7462 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7463 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7464 | } |
| 7465 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7466 | return INVALID_OPERATION; |
| 7467 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7468 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7469 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7470 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7471 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7472 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7473 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7474 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7475 | return status; |
| 7476 | } |
| 7477 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7478 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7479 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7480 | audio_format_t& format, |
| 7481 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7482 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7483 | { |
| 7484 | // Choose an input profile based on the requested capture parameters: select the first available |
| 7485 | // profile supporting all requested parameters. |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7486 | // The flags can be ignored if it doesn't contain a much match flag. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7487 | // |
| 7488 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 7489 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7490 | |
Atneya Nair | 0f0a803 | 2022-12-12 16:20:12 -0800 | [diff] [blame] | 7491 | using underlying_input_flag_t = std::underlying_type_t<audio_input_flags_t>; |
| 7492 | const underlying_input_flag_t mustMatchFlag = AUDIO_INPUT_FLAG_MMAP_NOIRQ | |
| 7493 | AUDIO_INPUT_FLAG_HOTWORD_TAP | AUDIO_INPUT_FLAG_HW_LOOKBACK; |
| 7494 | |
| 7495 | const underlying_input_flag_t oriFlags = flags; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7496 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7497 | for (;;) { |
| 7498 | sp<IOProfile> firstInexact = nullptr; |
| 7499 | uint32_t updatedSamplingRate = 0; |
| 7500 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 7501 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
| 7502 | for (const auto& hwModule : mHwModules) { |
| 7503 | for (const auto& profile : hwModule->getInputProfiles()) { |
| 7504 | // profile->log(); |
| 7505 | //updatedFormat = format; |
| 7506 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
| 7507 | &samplingRate /*updatedSamplingRate*/, |
| 7508 | format, |
| 7509 | &format, /*updatedFormat*/ |
| 7510 | channelMask, |
| 7511 | &channelMask /*updatedChannelMask*/, |
| 7512 | // FIXME ugly cast |
| 7513 | (audio_output_flags_t) flags, |
| 7514 | true /*exactMatchRequiredForInputFlags*/)) { |
| 7515 | return profile; |
| 7516 | } |
| 7517 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
| 7518 | samplingRate, |
| 7519 | &updatedSamplingRate, |
| 7520 | format, |
| 7521 | &updatedFormat, |
| 7522 | channelMask, |
| 7523 | &updatedChannelMask, |
| 7524 | // FIXME ugly cast |
| 7525 | (audio_output_flags_t) flags, |
| 7526 | false /*exactMatchRequiredForInputFlags*/)) { |
| 7527 | firstInexact = profile; |
| 7528 | } |
| 7529 | } |
| 7530 | } |
| 7531 | |
| 7532 | if (firstInexact != nullptr) { |
| 7533 | samplingRate = updatedSamplingRate; |
| 7534 | format = updatedFormat; |
| 7535 | channelMask = updatedChannelMask; |
| 7536 | return firstInexact; |
| 7537 | } else if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 7538 | flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_RAW); // retry |
| 7539 | } else if ((flags & mustMatchFlag) == AUDIO_INPUT_FLAG_NONE && |
| 7540 | flags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(format)) { |
| 7541 | flags = AUDIO_INPUT_FLAG_NONE; |
| 7542 | } else { // fail |
| 7543 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 7544 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 7545 | samplingRate, format, channelMask, oriFlags); |
| 7546 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7547 | } |
| 7548 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7549 | |
| 7550 | return nullptr; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7551 | } |
| 7552 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7553 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 7554 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 7555 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7556 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7557 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7558 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7559 | |
| 7560 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 7561 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 7562 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 7563 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7564 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7565 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 7566 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 7567 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 7568 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7569 | |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7570 | if (volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7571 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 7572 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 7573 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7574 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7575 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7576 | } |
| 7577 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 7578 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7579 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 7580 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7581 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7582 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 7583 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7584 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 7585 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 7586 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7587 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7588 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7589 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7590 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7591 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 7592 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7593 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 7594 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 7595 | // programmatically muted. |
| 7596 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 7597 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 7598 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7599 | bool exemptFromCapping = |
| 7600 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 7601 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7602 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 7603 | volumeSource, volumeDb); |
| 7604 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7605 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 7606 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 7607 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 7608 | } |
| 7609 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7610 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 7611 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 7612 | // - always attenuate notifications volume by 6dB |
| 7613 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 7614 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7615 | // - if music is playing, always limit the volume to current music volume, |
| 7616 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7617 | if (!Intersection(deviceTypes, |
| 7618 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 7619 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 7620 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 7621 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7622 | ((volumeSource == alarmVolumeSrc || |
| 7623 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7624 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 7625 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 7626 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7627 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 7628 | curves.canBeMuted()) { |
| 7629 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7630 | // when the phone is ringing we must consider that music could have been paused just before |
| 7631 | // by the music application and behave as if music was active if the last music track was |
| 7632 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7633 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7634 | mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7635 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7636 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7637 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 7638 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7639 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7640 | float musicVolDb = computeVolume(musicCurves, |
| 7641 | musicVolumeSrc, |
| 7642 | musicCurves.getVolumeIndex(musicDevice), |
| 7643 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7644 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 7645 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 7646 | if (volumeDb > minVolDb) { |
| 7647 | volumeDb = minVolDb; |
| 7648 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7649 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 7650 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 7651 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 7652 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7653 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 7654 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7655 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7656 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7657 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 7658 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7659 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 7660 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7661 | } |
| 7662 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7663 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7664 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7665 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7666 | } |
| 7667 | } |
| 7668 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7669 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7670 | } |
| 7671 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7672 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7673 | VolumeSource fromVolumeSource, |
| 7674 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7675 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7676 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7677 | return srcIndex; |
| 7678 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7679 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 7680 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7681 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 7682 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 7683 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 7684 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7685 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 7686 | // preserve mute request or correct range |
| 7687 | if (srcIndex < minSrc) { |
| 7688 | if (srcIndex == 0) { |
| 7689 | return 0; |
| 7690 | } |
| 7691 | srcIndex = minSrc; |
| 7692 | } else if (srcIndex > maxSrc) { |
| 7693 | srcIndex = maxSrc; |
| 7694 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7695 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 7696 | } |
| 7697 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7698 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 7699 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7700 | int index, |
| 7701 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7702 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7703 | int delayMs, |
| 7704 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7705 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7706 | // do not change actual attributes volume if the attributes is muted |
| 7707 | if (outputDesc->isMuted(volumeSource)) { |
| 7708 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 7709 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7710 | return NO_ERROR; |
| 7711 | } |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7712 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7713 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
| 7714 | bool isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 7715 | bool isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7716 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7717 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7718 | bool isHAUsed = isHearingAidUsedForComm(); |
| 7719 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7720 | // do not change in call volume if bluetooth is connected and vice versa |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7721 | // if sco and call follow same curves, bypass forceUseForComm |
| 7722 | if ((callVolSrc != btScoVolSrc) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7723 | ((isVoiceVolSrc && isScoRequested) || |
Beibei | f660a51 | 2023-02-28 17:00:34 +0800 | [diff] [blame] | 7724 | (isBtScoVolSrc && !(isScoRequested || isHAUsed))) && |
| 7725 | !isSingleDeviceType(deviceTypes, AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7726 | ALOGV("%s cannot set volume group %d volume when is%srequested for comm", __func__, |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7727 | volumeSource, isScoRequested ? " " : " not "); |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7728 | // Do not return an error here as AudioService will always set both voice call |
| 7729 | // and bluetooth SCO volumes due to stream aliasing. |
| 7730 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7731 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7732 | if (deviceTypes.empty()) { |
| 7733 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7734 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7735 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 7736 | if (curves.getVolumeIndexMin() < 0 || curves.getVolumeIndexMax() < 0) { |
| 7737 | ALOGE("invalid volume index range"); |
| 7738 | return BAD_VALUE; |
| 7739 | } |
| 7740 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7741 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 7742 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 7743 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 7744 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7745 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 7746 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7747 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7748 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7749 | outputDesc->setVolume( |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7750 | volumeDb, muted, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7751 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 7752 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7753 | float voiceVolume; |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7754 | // Force voice volume to max or mute for Bluetooth SCO as other attenuations are managed by the headset |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7755 | if (isVoiceVolSrc) { |
| 7756 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7757 | } else { |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7758 | voiceVolume = index == 0 ? 0.0 : 1.0; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7759 | } |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 7760 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7761 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 7762 | mLastVoiceVolume = voiceVolume; |
| 7763 | } |
| 7764 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7765 | return NO_ERROR; |
| 7766 | } |
| 7767 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7768 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7769 | const DeviceTypeSet& deviceTypes, |
| 7770 | int delayMs, |
| 7771 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7772 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 7773 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7774 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 7775 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 7776 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7777 | curves.getVolumeIndex(deviceTypes), |
| 7778 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7779 | } |
| 7780 | } |
| 7781 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7782 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 7783 | bool on, |
| 7784 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7785 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7786 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7787 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7788 | std::vector<VolumeSource> sourcesToMute; |
| 7789 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 7790 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 7791 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7792 | VolumeSource source = toVolumeSource(attributes, false); |
| 7793 | if ((source != VOLUME_SOURCE_NONE) && |
| 7794 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 7795 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7796 | sourcesToMute.push_back(source); |
| 7797 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7798 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7799 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7800 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7801 | } |
| 7802 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7803 | } |
| 7804 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7805 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 7806 | bool on, |
| 7807 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7808 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7809 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7810 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7811 | if (deviceTypes.empty()) { |
| 7812 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7813 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7814 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7815 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7816 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7817 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7818 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7819 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 7820 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7821 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7822 | } |
| 7823 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7824 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 7825 | // ignored |
| 7826 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7827 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7828 | if (!outputDesc->isMuted(volumeSource)) { |
| 7829 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7830 | return; |
| 7831 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7832 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 7833 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7834 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7835 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7836 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7837 | delayMs); |
| 7838 | } |
| 7839 | } |
| 7840 | } |
| 7841 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7842 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 7843 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7844 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7845 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 7846 | return true; |
| 7847 | } |
| 7848 | |
| 7849 | // has known usage? |
| 7850 | switch (paa->usage) { |
| 7851 | case AUDIO_USAGE_UNKNOWN: |
| 7852 | case AUDIO_USAGE_MEDIA: |
| 7853 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7854 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7855 | case AUDIO_USAGE_ALARM: |
| 7856 | case AUDIO_USAGE_NOTIFICATION: |
| 7857 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7858 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7859 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7860 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7861 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7862 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7863 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7864 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7865 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7866 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 7867 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 7868 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 7869 | case AUDIO_USAGE_EMERGENCY: |
| 7870 | case AUDIO_USAGE_SAFETY: |
| 7871 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 7872 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7873 | break; |
| 7874 | default: |
| 7875 | return false; |
| 7876 | } |
| 7877 | return true; |
| 7878 | } |
| 7879 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7880 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 7881 | { |
| 7882 | return mEngine->getForceUse(usage); |
| 7883 | } |
| 7884 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7885 | bool AudioPolicyManager::isInCall() const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7886 | return isStateInCall(mEngine->getPhoneState()); |
| 7887 | } |
| 7888 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7889 | bool AudioPolicyManager::isStateInCall(int state) const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7890 | return is_state_in_call(state); |
| 7891 | } |
| 7892 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7893 | bool AudioPolicyManager::isCallAudioAccessible() const { |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7894 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7895 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7896 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 7897 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7898 | } |
| 7899 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7900 | bool AudioPolicyManager::isInCallOrScreening() const { |
| 7901 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7902 | return isStateInCall(mode) || mode == AUDIO_MODE_CALL_SCREEN; |
| 7903 | } |
| 7904 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7905 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 7906 | { |
| 7907 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 7908 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7909 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 7910 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 7911 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7912 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7913 | } |
| 7914 | } |
| 7915 | |
| 7916 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 7917 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 7918 | bool release = false; |
| 7919 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 7920 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 7921 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 7922 | source->ext.device.type == deviceDesc->type()) { |
| 7923 | release = true; |
| 7924 | } |
| 7925 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7926 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7927 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 7928 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 7929 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7930 | sink->ext.device.type == deviceDesc->type() && |
| 7931 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 7932 | || strncmp(sink->ext.device.address, address, |
| 7933 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7934 | release = true; |
| 7935 | } |
| 7936 | } |
| 7937 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7938 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 7939 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7940 | } |
| 7941 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7942 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7943 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 7944 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7945 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7946 | } |
| 7947 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7948 | void AudioPolicyManager::modifySurroundFormats( |
| 7949 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7950 | std::unordered_set<audio_format_t> enforcedSurround( |
| 7951 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7952 | 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] | 7953 | for (const auto& pair : mConfig->getSurroundFormats()) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7954 | allSurround.insert(pair.first); |
| 7955 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 7956 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7957 | |
| 7958 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7959 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7960 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7961 | // This is the resulting set of formats depending on the surround mode: |
| 7962 | // 'all surround' = allSurround |
| 7963 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 7964 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 7965 | // 'manual surround' = mManualSurroundFormats |
| 7966 | // AUTO: formats v 'enforced surround' |
| 7967 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 7968 | // NEVER: formats ^ 'non-surround' |
| 7969 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7970 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7971 | std::unordered_set<audio_format_t> formatSet; |
| 7972 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 7973 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7974 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7975 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7976 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7977 | formatSet.insert(*formatIter); |
| 7978 | } |
| 7979 | } |
| 7980 | } else { |
| 7981 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 7982 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7983 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7984 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7985 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7986 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7987 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 7988 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 7989 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7990 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7991 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 7992 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 7993 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 7994 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7995 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7996 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7997 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7998 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7999 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8000 | } |
| 8001 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8002 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 8003 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8004 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 8005 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 8006 | |
| 8007 | // If NEVER, then remove support for channelMasks > stereo. |
| 8008 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8009 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 8010 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8011 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8012 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8013 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8014 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8015 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8016 | } |
| 8017 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8018 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 8019 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 8020 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8021 | bool supports5dot1 = false; |
| 8022 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8023 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8024 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 8025 | supports5dot1 = true; |
| 8026 | break; |
| 8027 | } |
| 8028 | } |
| 8029 | // If not then add 5.1 support. |
| 8030 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8031 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 8032 | 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] | 8033 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 8034 | } |
| 8035 | } |
| 8036 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8037 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8038 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8039 | AudioProfileVector &profiles) |
| 8040 | { |
| 8041 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8042 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 8043 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8044 | // Format MUST be checked first to update the list of AudioProfile |
| 8045 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8046 | reply = mpClientInterface->getParameters( |
| 8047 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 8048 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8049 | AudioParameter repliedParameters(reply); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8050 | FormatVector formats; |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8051 | if (repliedParameters.get( |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8052 | String8(AudioParameter::keyStreamSupportedFormats), reply) == NO_ERROR) { |
| 8053 | formats = formatsFromString(reply.string()); |
| 8054 | } else if (devDesc->hasValidAudioProfile()) { |
| 8055 | ALOGD("%s: using the device profiles", __func__); |
| 8056 | formats = devDesc->getAudioProfiles().getSupportedFormats(); |
| 8057 | } else { |
| 8058 | ALOGE("%s: failed to retrieve format, bailing out", __func__); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8059 | return; |
| 8060 | } |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 8061 | mReportedFormatsMap[devDesc] = formats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 8062 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 8063 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 8064 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 8065 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 8066 | addProfilesForFormats(profiles, formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8067 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8068 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 8069 | for (audio_format_t format : profiles.getSupportedFormats()) { |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8070 | std::optional<ChannelMaskSet> channelMasks; |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 8071 | SampleRateSet samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8072 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8073 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8074 | |
| 8075 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8076 | reply = mpClientInterface->getParameters( |
| 8077 | ioHandle, |
| 8078 | requestedParameters.toString() + ";" + |
| 8079 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8080 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8081 | AudioParameter repliedParameters(reply); |
| 8082 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8083 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8084 | samplingRates = samplingRatesFromString(reply.string()); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8085 | } else { |
| 8086 | samplingRates = devDesc->getAudioProfiles().getSampleRatesFor(format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8087 | } |
| 8088 | } |
| 8089 | if (profiles.hasDynamicChannelsFor(format)) { |
| 8090 | reply = mpClientInterface->getParameters(ioHandle, |
| 8091 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8092 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8093 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8094 | AudioParameter repliedParameters(reply); |
| 8095 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 8096 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 8097 | channelMasks = channelMasksFromString(reply.string()); |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8098 | } else { |
| 8099 | channelMasks = devDesc->getAudioProfiles().getChannelMasksFor(format); |
| 8100 | } |
| 8101 | if (channelMasks.has_value() && (device == AUDIO_DEVICE_OUT_HDMI |
| 8102 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD))) { |
| 8103 | modifySurroundChannelMasks(&channelMasks.value()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8104 | } |
| 8105 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 8106 | addDynamicAudioProfileAndSort( |
jiabin | f26596b | 2023-04-12 18:56:39 +0000 | [diff] [blame] | 8107 | profiles, new AudioProfile( |
| 8108 | format, channelMasks.value_or(ChannelMaskSet()), samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 8109 | } |
| 8110 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 8111 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8112 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8113 | audio_patch_handle_t *patchHandle, |
| 8114 | AudioIODescriptorInterface *ioDescriptor, |
| 8115 | const struct audio_patch *patch, |
| 8116 | int delayMs) |
| 8117 | { |
| 8118 | ssize_t index = mAudioPatches.indexOfKey( |
| 8119 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 8120 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 8121 | sp<AudioPatch> patchDesc; |
| 8122 | status_t status = installPatch( |
| 8123 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 8124 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8125 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8126 | } |
| 8127 | return status; |
| 8128 | } |
| 8129 | |
| 8130 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8131 | ssize_t index, |
| 8132 | audio_patch_handle_t *patchHandle, |
| 8133 | const struct audio_patch *patch, |
| 8134 | int delayMs, |
| 8135 | uid_t uid, |
| 8136 | sp<AudioPatch> *patchDescPtr) |
| 8137 | { |
| 8138 | sp<AudioPatch> patchDesc; |
| 8139 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 8140 | if (index >= 0) { |
| 8141 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8142 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8143 | } |
| 8144 | |
| 8145 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 8146 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 8147 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 8148 | if (status == NO_ERROR) { |
| 8149 | if (index < 0) { |
| 8150 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8151 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8152 | } else { |
| 8153 | patchDesc->mPatch = *patch; |
| 8154 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8155 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8156 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8157 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8158 | } |
| 8159 | nextAudioPortGeneration(); |
| 8160 | mpClientInterface->onAudioPatchListUpdate(); |
| 8161 | } |
| 8162 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 8163 | return status; |
| 8164 | } |
| 8165 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8166 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 8167 | { |
| 8168 | const TrackClientVector activeClients = output->getActiveClients(); |
| 8169 | if (activeClients.empty()) { |
| 8170 | return true; |
| 8171 | } |
| 8172 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 8173 | if (index < 0) { |
| 8174 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 8175 | __func__, output->getId()); |
| 8176 | return false; |
| 8177 | } |
| 8178 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 8179 | DeviceVector routedDevices; |
| 8180 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 8181 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 8182 | patchDesc->mPatch.sinks[i].id); |
| 8183 | if (device == nullptr) { |
| 8184 | ALOGE("%s, no audio device found with id(%d)", |
| 8185 | __func__, patchDesc->mPatch.sinks[i].id); |
| 8186 | return false; |
| 8187 | } |
| 8188 | routedDevices.add(device); |
| 8189 | } |
| 8190 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 8191 | if (client->isInvalid()) { |
| 8192 | // No need to take care about invalidated clients. |
| 8193 | continue; |
| 8194 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8195 | sp<DeviceDescriptor> preferredDevice = |
| 8196 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 8197 | if (mEngine->getOutputDevicesForAttributes( |
| 8198 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 8199 | return false; |
| 8200 | } |
| 8201 | } |
| 8202 | return true; |
| 8203 | } |
| 8204 | |
| 8205 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8206 | const sp<IOProfile>& profile, const DeviceVector& devices, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8207 | const audio_config_base_t *mixerConfig, const audio_config_t *halConfig, |
| 8208 | audio_output_flags_t flags) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8209 | { |
| 8210 | for (const auto& device : devices) { |
| 8211 | // TODO: This should be checking if the profile supports the device combo. |
| 8212 | if (!profile->supportsDevice(device)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8213 | ALOGE("%s profile(%s) doesn't support device %#x", __func__, profile->getName().c_str(), |
| 8214 | device->type()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8215 | return nullptr; |
| 8216 | } |
| 8217 | } |
| 8218 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 8219 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8220 | status_t status = desc->open(halConfig, mixerConfig, devices, |
| 8221 | AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8222 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8223 | ALOGE("%s failed to open output %d", __func__, status); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8224 | return nullptr; |
| 8225 | } |
| 8226 | |
| 8227 | // Here is where the out_set_parameters() for card & device gets called |
| 8228 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 8229 | const audio_devices_t deviceType = device->type(); |
| 8230 | const String8 &address = String8(device->address().c_str()); |
| 8231 | if (!address.isEmpty()) { |
| 8232 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 8233 | mpClientInterface->setParameters(output, String8(param)); |
| 8234 | free(param); |
| 8235 | } |
| 8236 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
| 8237 | if (!profile->hasValidAudioProfile()) { |
| 8238 | ALOGW("%s() missing param", __func__); |
| 8239 | desc->close(); |
| 8240 | return nullptr; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8241 | } else if (profile->hasDynamicAudioProfile() && halConfig == nullptr) { |
| 8242 | // Reopen the output with the best audio profile picked by APM when the profile supports |
| 8243 | // dynamic audio profile and the hal config is not specified. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8244 | desc->close(); |
| 8245 | output = AUDIO_IO_HANDLE_NONE; |
| 8246 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 8247 | profile->pickAudioProfile( |
| 8248 | config.sample_rate, config.channel_mask, config.format); |
| 8249 | config.offload_info.sample_rate = config.sample_rate; |
| 8250 | config.offload_info.channel_mask = config.channel_mask; |
| 8251 | config.offload_info.format = config.format; |
| 8252 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8253 | status = desc->open(&config, mixerConfig, devices, AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8254 | if (status != NO_ERROR) { |
| 8255 | return nullptr; |
| 8256 | } |
| 8257 | } |
| 8258 | |
| 8259 | addOutput(output, desc); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8260 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8261 | sp<DeviceDescriptor> speaker = mAvailableOutputDevices.getDevice( |
| 8262 | AUDIO_DEVICE_OUT_SPEAKER, String8(""), AUDIO_FORMAT_DEFAULT); |
| 8263 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8264 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 8265 | sp<AudioPolicyMix> policyMix; |
| 8266 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 8267 | policyMix->setOutput(desc); |
| 8268 | desc->mPolicyMix = policyMix; |
| 8269 | } else { |
| 8270 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
| 8271 | address.string()); |
| 8272 | } |
| 8273 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8274 | } else if (hasPrimaryOutput() && speaker != nullptr |
| 8275 | && mPrimaryOutput->supportsDevice(speaker) && !desc->supportsDevice(speaker) |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8276 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 8277 | // no duplicated output for: |
| 8278 | // - direct outputs |
| 8279 | // - outputs used by dynamic policy mixes |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8280 | // - outputs that supports SPEAKER while the primary output does not. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8281 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 8282 | |
| 8283 | //TODO: configure audio effect output stage here |
| 8284 | |
| 8285 | // open a duplicating output thread for the new output and the primary output |
| 8286 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 8287 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 8288 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 8289 | if (status == NO_ERROR) { |
| 8290 | // add duplicated output descriptor |
| 8291 | addOutput(duplicatedOutput, dupOutputDesc); |
| 8292 | } else { |
| 8293 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 8294 | mPrimaryOutput->mIoHandle, output); |
| 8295 | desc->close(); |
| 8296 | removeOutput(output); |
| 8297 | nextAudioPortGeneration(); |
| 8298 | return nullptr; |
| 8299 | } |
| 8300 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 8301 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 8302 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 8303 | mPrimaryOutput = desc; |
| 8304 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8305 | return desc; |
| 8306 | } |
| 8307 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8308 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 8309 | const audio_attributes_t &attr, DeviceVector &devices, bool forVolume) { |
| 8310 | // Devices are determined in the following precedence: |
| 8311 | // |
| 8312 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 8313 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 8314 | // |
| 8315 | // If no such dynamic policy then |
| 8316 | // 2) Devices containing an active client using setPreferredDevice |
| 8317 | // with same strategy as the attributes. |
| 8318 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8319 | // |
| 8320 | // If no corresponding active client with setPreferredDevice then |
| 8321 | // 3) Devices associated with the strategy determined by the attributes |
| 8322 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8323 | // |
| 8324 | // See related getOutputForAttrInt(). |
| 8325 | |
| 8326 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 8327 | // audio routing, only used for duplication for playback capture) |
| 8328 | sp<AudioPolicyMix> policyMix; |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8329 | bool unneededUsePrimaryOutputFromPolicyMixes = false; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8330 | status_t status = mPolicyMixes.getOutputForAttr(attr, AUDIO_CONFIG_BASE_INITIALIZER, |
Oscar Azucena | 873d10f | 2023-01-12 18:34:42 -0800 | [diff] [blame] | 8331 | 0 /*uid unknown here*/, AUDIO_SESSION_NONE, AUDIO_OUTPUT_FLAG_NONE, |
| 8332 | mAvailableOutputDevices, nullptr /* requestedDevice */, policyMix, |
| 8333 | nullptr /* secondaryMixes */, unneededUsePrimaryOutputFromPolicyMixes); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8334 | if (status != OK) { |
| 8335 | return status; |
| 8336 | } |
| 8337 | |
| 8338 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 8339 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 8340 | // as they are unaffected by device/stream volume |
| 8341 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 8342 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 8343 | ) { |
| 8344 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 8345 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 8346 | devices.add(deviceDesc); |
| 8347 | } else { |
| 8348 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 8349 | // which selects setPreferredDevice if active. This means forVolume call |
| 8350 | // will take an active setPreferredDevice, if such exists. |
| 8351 | |
| 8352 | devices = mEngine->getOutputDevicesForAttributes( |
| 8353 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
| 8354 | } |
| 8355 | |
| 8356 | if (forVolume) { |
| 8357 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 8358 | // for single volume control in AudioService (such relationship should exist if |
| 8359 | // SPEAKER_SAFE is present). |
| 8360 | // |
| 8361 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 8362 | DeviceVector speakerSafeDevices = |
| 8363 | devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 8364 | if (!speakerSafeDevices.isEmpty()) { |
| 8365 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 8366 | devices.remove(speakerSafeDevices); |
| 8367 | } |
| 8368 | } |
| 8369 | |
| 8370 | return NO_ERROR; |
| 8371 | } |
| 8372 | |
| 8373 | status_t AudioPolicyManager::getProfilesForDevices(const DeviceVector& devices, |
| 8374 | AudioProfileVector& audioProfiles, |
| 8375 | uint32_t flags, |
| 8376 | bool isInput) { |
| 8377 | for (const auto& hwModule : mHwModules) { |
| 8378 | // the MSD module checks for different conditions |
| 8379 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 8380 | continue; |
| 8381 | } |
| 8382 | IOProfileCollection ioProfiles = isInput ? hwModule->getInputProfiles() |
| 8383 | : hwModule->getOutputProfiles(); |
| 8384 | for (const auto& profile : ioProfiles) { |
| 8385 | if (!profile->areAllDevicesSupported(devices) || |
| 8386 | !profile->isCompatibleProfileForFlags( |
| 8387 | flags, false /*exactMatchRequiredForInputFlags*/)) { |
| 8388 | continue; |
| 8389 | } |
| 8390 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8391 | } |
| 8392 | } |
| 8393 | |
| 8394 | if (!isInput) { |
| 8395 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 8396 | const auto &msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 8397 | if (msdModule != nullptr) { |
| 8398 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 8399 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 8400 | for (const auto &profile: msdModule->getOutputProfiles()) { |
| 8401 | if (!profile->asAudioPort()->isDirectOutput()) { |
| 8402 | continue; |
| 8403 | } |
| 8404 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8405 | } |
| 8406 | } else { |
| 8407 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 8408 | } |
| 8409 | } |
| 8410 | } |
| 8411 | |
| 8412 | return NO_ERROR; |
| 8413 | } |
| 8414 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8415 | sp<SwAudioOutputDescriptor> AudioPolicyManager::reopenOutput(sp<SwAudioOutputDescriptor> outputDesc, |
| 8416 | const audio_config_t *config, |
| 8417 | audio_output_flags_t flags, |
| 8418 | const char* caller) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8419 | closeOutput(outputDesc->mIoHandle); |
| 8420 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 8421 | outputDesc->mProfile, outputDesc->devices(), nullptr /*mixerConfig*/, config, flags); |
| 8422 | if (preferredOutput == nullptr) { |
| 8423 | ALOGE("%s failed to reopen output device=%d, caller=%s", |
| 8424 | __func__, outputDesc->devices()[0]->getId(), caller); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8425 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8426 | return preferredOutput; |
| 8427 | } |
| 8428 | |
| 8429 | void AudioPolicyManager::reopenOutputsWithDevices( |
| 8430 | const std::map<audio_io_handle_t, DeviceVector> &outputsToReopen) { |
| 8431 | for (const auto& [output, devices] : outputsToReopen) { |
| 8432 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 8433 | closeOutput(output); |
| 8434 | openOutputWithProfileAndDevice(desc->mProfile, devices); |
| 8435 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8436 | } |
| 8437 | |
jiabin | c44b346 | 2022-12-08 12:52:31 -0800 | [diff] [blame] | 8438 | PortHandleVector AudioPolicyManager::getClientsForStream( |
| 8439 | audio_stream_type_t streamType) const { |
| 8440 | PortHandleVector clients; |
| 8441 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 8442 | PortHandleVector clientsForStream = mOutputs.valueAt(i)->getClientsForStream(streamType); |
| 8443 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8444 | } |
| 8445 | return clients; |
| 8446 | } |
| 8447 | |
| 8448 | void AudioPolicyManager::invalidateStreams(StreamTypeVector streams) const { |
| 8449 | PortHandleVector clients; |
| 8450 | for (auto stream : streams) { |
| 8451 | PortHandleVector clientsForStream = getClientsForStream(stream); |
| 8452 | clients.insert(clients.end(), clientsForStream.begin(), clientsForStream.end()); |
| 8453 | } |
| 8454 | mpClientInterface->invalidateTracks(clients); |
| 8455 | } |
| 8456 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 8457 | } // namespace android |