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> |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 37 | #include <unordered_set> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 38 | #include <vector> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 39 | |
| 40 | #include <Serializer.h> |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 41 | #include <android/media/audio/common/AudioPort.h> |
Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 42 | #include <cutils/bitops.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 43 | #include <cutils/properties.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 44 | #include <media/AudioParameter.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 45 | #include <policy.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 47 | #include <system/audio.h> |
Mikhail Naganov | 27cf37c | 2020-04-14 14:47:01 -0700 | [diff] [blame] | 48 | #include <system/audio_config.h> |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 49 | #include <system/audio_effects/effect_hapticgenerator.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 50 | #include <utils/Log.h> |
| 51 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 52 | #include "AudioPolicyManager.h" |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 53 | #include "TypeConverter.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 54 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 55 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 56 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 57 | using android::media::audio::common::AudioDevice; |
| 58 | using android::media::audio::common::AudioDeviceAddress; |
| 59 | using android::media::audio::common::AudioPortDeviceExt; |
| 60 | using android::media::audio::common::AudioPortExt; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 61 | using content::AttributionSourceState; |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 62 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 63 | //FIXME: workaround for truncated touch sounds |
| 64 | // to be removed when the problem is handled by system UI |
| 65 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 66 | |
| 67 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 68 | // media / notification / system volume. |
| 69 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 70 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 71 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 72 | static const std::vector<audio_format_t> msdCompressedFormatsOrder = {{ |
| 73 | AUDIO_FORMAT_IEC60958, AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 74 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 75 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 76 | static const std::vector<audio_channel_mask_t> msdSurroundChannelMasksOrder = {{ |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 77 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 78 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 79 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 80 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 81 | template <typename T> |
| 82 | bool operator== (const SortedVector<T> &left, const SortedVector<T> &right) |
| 83 | { |
| 84 | if (left.size() != right.size()) { |
| 85 | return false; |
| 86 | } |
| 87 | for (size_t index = 0; index < right.size(); index++) { |
| 88 | if (left[index] != right[index]) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | template <typename T> |
| 96 | bool operator!= (const SortedVector<T> &left, const SortedVector<T> &right) |
| 97 | { |
| 98 | return !(left == right); |
| 99 | } |
| 100 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 101 | // ---------------------------------------------------------------------------- |
| 102 | // AudioPolicyInterface implementation |
| 103 | // ---------------------------------------------------------------------------- |
| 104 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 105 | status_t AudioPolicyManager::setDeviceConnectionState(audio_policy_dev_state_t state, |
| 106 | const android::media::audio::common::AudioPort& port, audio_format_t encodedFormat) { |
| 107 | status_t status = setDeviceConnectionStateInt(state, port, encodedFormat); |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 108 | nextAudioPortGeneration(); |
| 109 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 112 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
| 113 | audio_policy_dev_state_t state, |
| 114 | const char* device_address, |
| 115 | const char* device_name, |
| 116 | audio_format_t encodedFormat) { |
| 117 | media::AudioPort aidlPort; |
| 118 | if (status_t status = deviceToAudioPort(device, device_address, device_name, &aidlPort); |
| 119 | status == OK) { |
| 120 | return setDeviceConnectionState(state, aidlPort.hal, encodedFormat); |
| 121 | } else { |
| 122 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 123 | return status; |
| 124 | } |
| 125 | } |
| 126 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 127 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
| 128 | audio_policy_dev_state_t state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 129 | { |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 130 | audio_port_v7 devicePort; |
| 131 | device->toAudioPort(&devicePort); |
| 132 | if (status_t status = mpClientInterface->setDeviceConnectedState( |
| 133 | &devicePort, state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 134 | status != OK) { |
| 135 | ALOGE("Error %d while setting connected state for device %s", status, |
| 136 | device->getDeviceTypeAddr().toString(false).c_str()); |
| 137 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 140 | status_t AudioPolicyManager::setDeviceConnectionStateInt( |
| 141 | audio_policy_dev_state_t state, const android::media::audio::common::AudioPort& port, |
| 142 | audio_format_t encodedFormat) { |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 143 | if (port.ext.getTag() != AudioPortExt::device) { |
| 144 | return BAD_VALUE; |
| 145 | } |
| 146 | audio_devices_t device_type; |
| 147 | std::string device_address; |
| 148 | if (status_t status = aidl2legacy_AudioDevice_audio_device( |
| 149 | port.ext.get<AudioPortExt::device>().device, &device_type, &device_address); |
| 150 | status != OK) { |
| 151 | return status; |
| 152 | }; |
| 153 | const char* device_name = port.name.c_str(); |
| 154 | // connect/disconnect only 1 device at a time |
| 155 | if (!audio_is_output_device(device_type) && !audio_is_input_device(device_type)) |
| 156 | return BAD_VALUE; |
| 157 | |
| 158 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 159 | device_type, device_address.c_str(), device_name, encodedFormat, |
| 160 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 161 | if (device == nullptr) { |
| 162 | return INVALID_OPERATION; |
| 163 | } |
| 164 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 165 | device->setExtraAudioDescriptors(port.extraAudioDescriptors); |
| 166 | } |
| 167 | return setDeviceConnectionStateInt(device, state); |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 168 | } |
| 169 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 170 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 171 | audio_policy_dev_state_t state, |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 172 | const char* device_address, |
| 173 | const char* device_name, |
| 174 | audio_format_t encodedFormat) { |
| 175 | media::AudioPort aidlPort; |
| 176 | if (status_t status = deviceToAudioPort(deviceType, device_address, device_name, &aidlPort); |
| 177 | status == OK) { |
| 178 | return setDeviceConnectionStateInt(state, aidlPort.hal, encodedFormat); |
| 179 | } else { |
| 180 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 181 | return status; |
| 182 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 183 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 184 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 185 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 186 | audio_policy_dev_state_t state) |
| 187 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 188 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 189 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 190 | SortedVector <audio_io_handle_t> outputs; |
| 191 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 192 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 193 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 194 | // save a copy of the opened output descriptors before any output is opened or closed |
| 195 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 196 | mPreviousOutputs = mOutputs; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 197 | |
| 198 | bool wasLeUnicastActive = isLeUnicastActive(); |
| 199 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | switch (state) |
| 201 | { |
| 202 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 203 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 204 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 205 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 206 | return INVALID_OPERATION; |
| 207 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 208 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 209 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 210 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 211 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 212 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 213 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 214 | } |
| 215 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 216 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 217 | // parameters on newly connected devices (instead of opening the outputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 218 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 219 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 220 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 221 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 222 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 223 | mHwModules.cleanUpForDevice(device); |
| 224 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 225 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 226 | return INVALID_OPERATION; |
| 227 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 228 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 229 | // Populate encapsulation information when a output device is connected. |
| 230 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 231 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 232 | // outputs should never be empty here |
| 233 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 234 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 235 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 236 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 237 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 238 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 239 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 240 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 241 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 242 | return INVALID_OPERATION; |
| 243 | } |
| 244 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 245 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 246 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 247 | // Send Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 248 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 249 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 250 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 251 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 252 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 253 | mOutputs.clearSessionRoutesForDevice(device); |
| 254 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 255 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 256 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 257 | // Reset active device codec |
| 258 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 259 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 260 | // remove device from mReportedFormatsMap cache |
| 261 | mReportedFormatsMap.erase(device); |
| 262 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 263 | // remove preferred mixer configurations |
| 264 | mPreferredMixerAttrInfos.erase(device->getId()); |
| 265 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 266 | } break; |
| 267 | |
| 268 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 269 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 270 | return BAD_VALUE; |
| 271 | } |
| 272 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 273 | // Propagate device availability to Engine |
| 274 | setEngineDeviceConnectionState(device, state); |
| 275 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 276 | // No need to evaluate playback routing when connecting a remote submix |
| 277 | // output device used by a dynamic policy of type recorder as no |
| 278 | // playback use case is affected. |
| 279 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 280 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 281 | for (audio_io_handle_t output : outputs) { |
| 282 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 283 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 284 | if (policyMix != nullptr |
| 285 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 286 | && device->address() == policyMix->mDeviceAddress.string()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 287 | doCheckForDeviceAndOutputChanges = false; |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 294 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 295 | if (!outputs.isEmpty()) { |
| 296 | for (audio_io_handle_t output : outputs) { |
| 297 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 298 | // close unused outputs after device disconnection or direct outputs that have |
| 299 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 300 | // "outputs" vector never contains duplicated outputs |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 301 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 302 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 303 | (desc->mDirectOpenCount == 0)) |
| 304 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) && |
| 305 | !isOutputOnlyAvailableRouteToSomeDevice(desc))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 306 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 307 | closeOutput(output); |
| 308 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 309 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 310 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 311 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 313 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | if (doCheckForDeviceAndOutputChanges) { |
| 317 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 318 | } else { |
| 319 | checkCloseOutputs(); |
| 320 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 321 | (void)updateCallRouting(false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 322 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 323 | const DeviceVector activeMediaDevices = |
| 324 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 325 | std::map<audio_io_handle_t, DeviceVector> outputsToReopenWithDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 326 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 327 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 328 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 329 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 330 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 331 | // do not force device change on duplicated output because if device is 0, it will |
| 332 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 333 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 334 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 335 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 336 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 337 | // always force when disconnecting (a non-duplicated device) |
| 338 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 339 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 340 | // If the device is using preferred mixer attributes, the output need to reopen |
| 341 | // with default configuration when the new selected devices are different from |
| 342 | // current routing devices |
| 343 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), newDevices); |
| 344 | continue; |
| 345 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 346 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 347 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 348 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 349 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 350 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 351 | // Reopen the output to query the dynamic profiles when there is not active |
| 352 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 353 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 354 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 355 | if (areAllActiveTracksRerouted(desc)) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 356 | outputsToReopenWithDevices.emplace(mOutputs.keyAt(i), activeMediaDevices); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 357 | } else { |
| 358 | desc->mPendingReopenToQueryProfiles = true; |
| 359 | } |
| 360 | } |
| 361 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 362 | // Clear the flag that previously set for re-querying profiles. |
| 363 | desc->mPendingReopenToQueryProfiles = false; |
| 364 | } |
| 365 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 366 | reopenOutputsWithDevices(outputsToReopenWithDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 367 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 368 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 369 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 372 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, 0); |
| 373 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 374 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 375 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 376 | } // end if is output device |
| 377 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 378 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 379 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 380 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 381 | switch (state) |
| 382 | { |
| 383 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 384 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 385 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 386 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 387 | return INVALID_OPERATION; |
| 388 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 389 | |
| 390 | if (mAvailableInputDevices.add(device) < 0) { |
| 391 | return NO_MEMORY; |
| 392 | } |
| 393 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 394 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 395 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 396 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 397 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 398 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 399 | mAvailableInputDevices.remove(device); |
| 400 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 401 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 402 | |
| 403 | mHwModules.cleanUpForDevice(device); |
| 404 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 405 | return INVALID_OPERATION; |
| 406 | } |
| 407 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 408 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 409 | |
| 410 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 411 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 412 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 413 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 414 | return INVALID_OPERATION; |
| 415 | } |
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 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 418 | |
| 419 | // Set Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 420 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 421 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 422 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 423 | |
| 424 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 425 | |
| 426 | // remove device from mReportedFormatsMap cache |
| 427 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 428 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 429 | |
| 430 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 431 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 432 | return BAD_VALUE; |
| 433 | } |
| 434 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 435 | // Propagate device availability to Engine |
| 436 | setEngineDeviceConnectionState(device, state); |
| 437 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 438 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 439 | // As the input device list can impact the output device selection, update |
| 440 | // getDeviceForStrategy() cache |
| 441 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 442 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 443 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 444 | // Reconnect Audio Source |
| 445 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 446 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 447 | checkAudioSourceForAttributes(attributes); |
| 448 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 449 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 450 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 453 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 454 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 455 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 456 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 457 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 458 | return BAD_VALUE; |
| 459 | } |
| 460 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 461 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 462 | const char* device_name, |
| 463 | media::AudioPort* aidlPort) { |
| 464 | DeviceDescriptorBase devDescr(device, device_address); |
| 465 | devDescr.setName(device_name); |
| 466 | return devDescr.writeToParcelable(aidlPort); |
| 467 | } |
| 468 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 469 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 470 | audio_policy_dev_state_t state) { |
| 471 | |
| 472 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 473 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 474 | return; |
| 475 | } |
| 476 | mEngine->setDeviceConnectionState(device, state); |
| 477 | } |
| 478 | |
| 479 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 480 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 481 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 482 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 483 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 484 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 485 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 486 | (strlen(device_address) != 0)/*matchAddress*/); |
| 487 | |
| 488 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 489 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 490 | device, device_address); |
| 491 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 492 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 493 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 494 | DeviceVector *deviceVector; |
| 495 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 496 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 497 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 498 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 499 | deviceVector = &mAvailableInputDevices; |
| 500 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 501 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 502 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 503 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 504 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 505 | return (deviceVector->getDevice( |
| 506 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 507 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 510 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 511 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 512 | const char *device_name, |
| 513 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 514 | { |
| 515 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 516 | String8 reply; |
| 517 | AudioParameter param; |
| 518 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 519 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 520 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 521 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 522 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 523 | // connect/disconnect only 1 device at a time |
| 524 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 525 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 526 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 527 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 528 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 529 | // Nothing to do: device is not connected |
| 530 | return NO_ERROR; |
| 531 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 532 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 533 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 534 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 535 | // configure codecs. |
| 536 | // Handle two specific cases by sending a set parameter to |
| 537 | // configure A2DP codecs. No need to toggle device state. |
| 538 | // Case 1: A2DP active device switches from primary to primary |
| 539 | // module |
| 540 | // Case 2: A2DP device config changes on primary module. |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 541 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 542 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 543 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 544 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 545 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 546 | reply = mpClientInterface->getParameters( |
| 547 | AUDIO_IO_HANDLE_NONE, |
| 548 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 549 | AudioParameter repliedParameters(reply); |
| 550 | repliedParameters.getInt( |
| 551 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 552 | if (isReconfigA2dpSupported) { |
| 553 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 554 | param.add(key, String8("true")); |
| 555 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 556 | devDesc->setEncodedFormat(encodedFormat); |
| 557 | return NO_ERROR; |
| 558 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 561 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 562 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 563 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 564 | // mute media strategies and delay device switch by the largest |
| 565 | // This avoid sending the music tail into the earpiece or headset. |
| 566 | setStrategyMute(musicStrategy, true, desc); |
| 567 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 568 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 569 | nullptr, true /*fromCache*/).types()); |
| 570 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 571 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 572 | // This will force reading again the device configuration |
| 573 | status = setDeviceConnectionState(device, |
| 574 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 575 | device_address, device_name, |
| 576 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 577 | if (status != NO_ERROR) { |
| 578 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 579 | status); |
| 580 | return status; |
| 581 | } |
| 582 | |
| 583 | status = setDeviceConnectionState(device, |
| 584 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 585 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 586 | if (status != NO_ERROR) { |
| 587 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 588 | status); |
| 589 | return status; |
| 590 | } |
| 591 | |
| 592 | return NO_ERROR; |
| 593 | } |
| 594 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 595 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 596 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 597 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 598 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 599 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 600 | std::unordered_set<audio_format_t> formatSet; |
| 601 | sp<HwModule> primaryModule = |
| 602 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 603 | if (primaryModule == nullptr) { |
| 604 | ALOGE("%s() unable to get primary module", __func__); |
| 605 | return NO_INIT; |
| 606 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 607 | |
| 608 | DeviceTypeSet audioDeviceSet; |
| 609 | |
| 610 | switch(device) { |
| 611 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 612 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 613 | break; |
| 614 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
| 615 | audioDeviceSet = getAudioDeviceOutAllBleSet(); |
| 616 | break; |
| 617 | default: |
| 618 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 619 | return BAD_VALUE; |
| 620 | } |
| 621 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 622 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 623 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 624 | for (const auto& device : declaredDevices) { |
| 625 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 626 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 627 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 628 | return status; |
| 629 | } |
| 630 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 631 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 632 | { |
| 633 | DeviceVector rxSinkdevices{}; |
| 634 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 635 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 636 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 637 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 638 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 639 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 640 | // retrieve Rx Source device descriptor |
| 641 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 642 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 643 | |
| 644 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 645 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 646 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 647 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 648 | return DeviceVector(rxSinkDevice); |
| 649 | } |
| 650 | } |
| 651 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 652 | // the device returned is not necessarily reachable via this output |
| 653 | // (filter later by setOutputDevices()) |
| 654 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 655 | } |
| 656 | |
| 657 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 658 | { |
| 659 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
| 660 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 661 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 662 | } |
| 663 | return INVALID_OPERATION; |
| 664 | } |
| 665 | |
| 666 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 667 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 668 | { |
| 669 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 670 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 671 | uint32_t muteWaitMs = 0; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 672 | if(!hasPrimaryOutput() || |
| 673 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 674 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 675 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 676 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 677 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 678 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 679 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 680 | ALOG_ASSERT(txSourceDevice != 0, "%s() input selected device not available", __func__); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 681 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 682 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 683 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 684 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 685 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 686 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 687 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 688 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 689 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 690 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 691 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 692 | // retrieve Rx Source and Tx Sink device descriptors |
| 693 | sp<DeviceDescriptor> rxSourceDevice = |
| 694 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 695 | String8(), |
| 696 | AUDIO_FORMAT_DEFAULT); |
| 697 | sp<DeviceDescriptor> txSinkDevice = |
| 698 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 699 | String8(), |
| 700 | AUDIO_FORMAT_DEFAULT); |
| 701 | |
| 702 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 703 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 704 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 705 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 706 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 707 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 708 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 709 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 710 | // createAudioPatchInternal now supports both HW / SW bridging |
| 711 | createRxPatch = true; |
| 712 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 713 | } else { |
| 714 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 715 | // voice calls via setOutputDevice() on primary output. |
| 716 | // Otherwise, create two audio patches for TX and RX path. |
| 717 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 718 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 719 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 720 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 721 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 722 | (txSinkDevice != 0); |
| 723 | } |
| 724 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 725 | // Otherwise, create two audio patches for TX and RX path. |
| 726 | if (!createRxPatch) { |
| 727 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 728 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 729 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 730 | // If the TX device is on the primary HW module but RX device is |
| 731 | // on other HW module, SinkMetaData of telephony input should handle it |
| 732 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 733 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 734 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 735 | // terminate active capture if on the same HW module as the call TX source device |
| 736 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 737 | // call TX device but this information is not in the audio patch and logic here must be |
| 738 | // symmetric to the one in startInput() |
| 739 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 740 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 741 | closeActiveClients(activeDesc); |
| 742 | } |
| 743 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 744 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 745 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 746 | if (waitMs != nullptr) { |
| 747 | *waitMs = muteWaitMs; |
| 748 | } |
| 749 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 750 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 751 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 752 | bool AudioPolicyManager::isDeviceOfModule( |
| 753 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 754 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 755 | if (module != 0) { |
| 756 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 757 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 758 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 759 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 760 | } |
| 761 | return false; |
| 762 | } |
| 763 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 764 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 765 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 766 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 767 | const struct audio_port_config source = { |
| 768 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 769 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 770 | }; |
| 771 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 772 | mCallRxSourceClient = startAudioSourceInternal(&source, &aa, 0/*uid*/); |
| 773 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 774 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 777 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 778 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 779 | if (clientDesc == nullptr) { |
| 780 | return; |
| 781 | } |
| 782 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 783 | "%s error stopping audio source", __func__); |
| 784 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 788 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 789 | uint32_t delayMs) |
| 790 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 791 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 792 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 793 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 794 | return; |
| 795 | } |
| 796 | PatchBuilder patchBuilder; |
| 797 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
| 798 | ALOGV("%s between source %s and sink %s", __func__, |
| 799 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 800 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 801 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 802 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 803 | struct audio_port_config source = {}; |
| 804 | srcDevice->toAudioPortConfig(&source); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 805 | mCallTxSourceClient = new InternalSourceClientDescriptor( |
| 806 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, sinkDevice, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 807 | mCommunnicationStrategy, toVolumeSource(aa)); |
| 808 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 809 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 810 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 811 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 812 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
| 813 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 814 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 815 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 818 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 819 | { |
| 820 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 821 | // store previous phone state for management of sonification strategy below |
| 822 | int oldState = mEngine->getPhoneState(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 823 | bool wasLeUnicastActive = isLeUnicastActive(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 824 | |
| 825 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 826 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 827 | return; |
| 828 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 829 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 830 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 831 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 832 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 833 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 834 | } |
| 835 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 836 | /** |
| 837 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 838 | * routing command. |
| 839 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 840 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 841 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 842 | |
| 843 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 844 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 845 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 846 | int delayMs = 0; |
| 847 | if (isStateInCall(state)) { |
| 848 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 849 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 850 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 851 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 852 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 853 | // mute media and sonification strategies and delay device switch by the largest |
| 854 | // latency of any output where either strategy is active. |
| 855 | // 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] | 856 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 857 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 858 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 859 | (delayMs < (int)desc->latency()*2)) { |
| 860 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 861 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 862 | setStrategyMute(musicStrategy, true, desc); |
| 863 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 864 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 865 | nullptr, true /*fromCache*/).types()); |
| 866 | setStrategyMute(sonificationStrategy, true, desc); |
| 867 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 868 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 869 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 873 | if (hasPrimaryOutput()) { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 874 | if (state == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 875 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 876 | } else { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 877 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 878 | // force routing command to audio hardware when ending call |
| 879 | // even if no device change is needed |
| 880 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 881 | rxDevices = mPrimaryOutput->devices(); |
| 882 | } |
| 883 | if (oldState == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 884 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 885 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 886 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 887 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 888 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 889 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 890 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 891 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 892 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 893 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 894 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 895 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 896 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 897 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 898 | if (desc->mUsePreferredMixerAttributes && newDevices != desc->devices()) { |
| 899 | // If the device is using preferred mixer attributes, the output need to reopen |
| 900 | // with default configuration when the new selected devices are different from |
| 901 | // current routing devices. |
| 902 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 903 | continue; |
| 904 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 905 | setOutputDevices(desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
| 906 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 909 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 910 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 911 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
| 912 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 913 | if (isStateInCall(state)) { |
| 914 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 915 | // force reevaluating accessibility routing when call starts |
| 916 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | // 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] | 920 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 921 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 924 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 925 | return mEngine->getPhoneState(); |
| 926 | } |
| 927 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 928 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 929 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 930 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 931 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 932 | if (config == mEngine->getForceUse(usage)) { |
| 933 | return; |
| 934 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 935 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 936 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 937 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 938 | return; |
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 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 941 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 942 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 | |
| 944 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 945 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 946 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 947 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 948 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
| 949 | mpClientInterface->invalidateStream(AUDIO_STREAM_SYSTEM); |
| 950 | mpClientInterface->invalidateStream(AUDIO_STREAM_ENFORCED_AUDIBLE); |
| 951 | } |
| 952 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 953 | //FIXME: workaround for truncated touch sounds |
| 954 | // to be removed when the problem is handled by system UI |
| 955 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 956 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 957 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 958 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 959 | |
| 960 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 961 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 964 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 965 | { |
| 966 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 967 | } |
| 968 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 969 | // Find an MSD output profile compatible with the parameters passed. |
| 970 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 971 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 972 | const DeviceVector& devices, |
| 973 | uint32_t samplingRate, |
| 974 | audio_format_t format, |
| 975 | audio_channel_mask_t channelMask, |
| 976 | audio_output_flags_t flags, |
| 977 | bool directOnly) |
| 978 | { |
| 979 | flags = getRelevantFlags(flags, directOnly); |
| 980 | |
| 981 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 982 | if (msdModule != nullptr) { |
| 983 | // for the msd module check if there are patches to the output devices |
| 984 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 985 | HwModuleCollection modules; |
| 986 | modules.add(msdModule); |
| 987 | return searchCompatibleProfileHwModules( |
| 988 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 989 | flags, directOnly); |
| 990 | } |
| 991 | } |
| 992 | return nullptr; |
| 993 | } |
| 994 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 995 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 996 | // search to profiles for direct outputs. |
| 997 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 998 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 999 | uint32_t samplingRate, |
| 1000 | audio_format_t format, |
| 1001 | audio_channel_mask_t channelMask, |
| 1002 | audio_output_flags_t flags, |
| 1003 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1004 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1005 | flags = getRelevantFlags(flags, directOnly); |
| 1006 | |
| 1007 | return searchCompatibleProfileHwModules( |
| 1008 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 1009 | } |
| 1010 | |
| 1011 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 1012 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1013 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1014 | // only retain flags that will drive the direct output profile selection |
| 1015 | // if explicitly requested |
| 1016 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1017 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1018 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1019 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1020 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1021 | return flags; |
| 1022 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1023 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1024 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 1025 | const HwModuleCollection& hwModules, |
| 1026 | const DeviceVector& devices, |
| 1027 | uint32_t samplingRate, |
| 1028 | audio_format_t format, |
| 1029 | audio_channel_mask_t channelMask, |
| 1030 | audio_output_flags_t flags, |
| 1031 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1032 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1033 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1034 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1035 | if (!curProfile->isCompatibleProfile(devices, |
| 1036 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1037 | format, NULL /*updatedFormat*/, |
| 1038 | channelMask, NULL /*updatedChannelMask*/, |
| 1039 | flags)) { |
| 1040 | continue; |
| 1041 | } |
| 1042 | // reject profiles not corresponding to a device currently available |
| 1043 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1044 | continue; |
| 1045 | } |
| 1046 | // reject profiles if connected device does not support codec |
| 1047 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1048 | continue; |
| 1049 | } |
| 1050 | if (!directOnly) { |
| 1051 | return curProfile; |
| 1052 | } |
| 1053 | |
| 1054 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1055 | // to one with offload capability |
| 1056 | if (profile != 0 && |
| 1057 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1058 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1059 | } |
| 1060 | profile = curProfile; |
| 1061 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1062 | break; |
| 1063 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1064 | } |
| 1065 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1066 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1069 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1070 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1071 | { |
| 1072 | for (const auto& hwModule : mHwModules) { |
| 1073 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1074 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1075 | continue; |
| 1076 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1077 | if (!devices.empty()) { |
Eric Laurent | 0c8f7cc | 2022-06-24 14:32:36 +0200 | [diff] [blame] | 1078 | // reject profiles not corresponding to a device currently available |
| 1079 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1080 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1081 | continue; |
| 1082 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1083 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1084 | != devices.size()) { |
| 1085 | continue; |
| 1086 | } |
| 1087 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1088 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1089 | return curProfile; |
| 1090 | } |
| 1091 | } |
| 1092 | return nullptr; |
| 1093 | } |
| 1094 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1095 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1096 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1097 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1098 | |
| 1099 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1100 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1101 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1102 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1103 | // and AudioSystem::getOutputSamplingRate(). |
| 1104 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1105 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1106 | const audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1107 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1108 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1109 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1110 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1113 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1114 | const audio_attributes_t *srcAttr, |
| 1115 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1116 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1117 | if (srcAttr != NULL) { |
| 1118 | if (!isValidAttributes(srcAttr)) { |
| 1119 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1120 | __func__, |
| 1121 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1122 | srcAttr->tags); |
| 1123 | return BAD_VALUE; |
| 1124 | } |
| 1125 | *dstAttr = *srcAttr; |
| 1126 | } else { |
| 1127 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1128 | ALOGE("%s: invalid stream type", __func__); |
| 1129 | return BAD_VALUE; |
| 1130 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1131 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1132 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1133 | |
| 1134 | // Only honor audibility enforced when required. The client will be |
| 1135 | // forced to reconnect if the forced usage changes. |
| 1136 | 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] | 1137 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1138 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1141 | return NO_ERROR; |
| 1142 | } |
| 1143 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1144 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1145 | audio_attributes_t *resultAttr, |
| 1146 | audio_io_handle_t *output, |
| 1147 | audio_session_t session, |
| 1148 | const audio_attributes_t *attr, |
| 1149 | audio_stream_type_t *stream, |
| 1150 | uid_t uid, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1151 | audio_config_t *config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1152 | audio_output_flags_t *flags, |
| 1153 | audio_port_handle_t *selectedDeviceId, |
| 1154 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1155 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1156 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1157 | bool *isSpatialized, |
| 1158 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1159 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1160 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1161 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1162 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1163 | const sp<DeviceDescriptor> requestedDevice = |
| 1164 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1165 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1166 | *outputType = API_OUTPUT_INVALID; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1167 | *isSpatialized = false; |
| 1168 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1169 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1170 | if (status != NO_ERROR) { |
| 1171 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1172 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1173 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1174 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1175 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1176 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1177 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1178 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1179 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1180 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1181 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1182 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1183 | // 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] | 1184 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1185 | const audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1186 | .channel_mask = config->channel_mask, |
| 1187 | .format = config->format, |
| 1188 | }; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 1189 | status = mPolicyMixes.getOutputForAttr(*resultAttr, clientConfig, uid, session, *flags, |
| 1190 | primaryMix, secondaryMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1191 | if (status != OK) { |
| 1192 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1193 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1194 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1195 | // Explicit routing is higher priority then any dynamic policy primary output |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1196 | bool usePrimaryOutputFromPolicyMixes = requestedDevice == nullptr && primaryMix != nullptr; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1197 | |
| 1198 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1199 | if ((secondaryMixes != nullptr && !secondaryMixes->empty()) |
| 1200 | && !audio_is_linear_pcm(config->format)) { |
| 1201 | ALOGD("%s: rejecting request as secondary mixes only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1202 | return BAD_VALUE; |
| 1203 | } |
| 1204 | if (usePrimaryOutputFromPolicyMixes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1205 | sp<DeviceDescriptor> deviceDesc = |
| 1206 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1207 | primaryMix->mDeviceAddress, |
| 1208 | AUDIO_FORMAT_DEFAULT); |
| 1209 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1210 | bool tryDirectForFlags = policyDesc == nullptr || |
| 1211 | (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT); |
| 1212 | // if a direct output can be opened to deliver the track's multi-channel content to the |
| 1213 | // output rather than being downmixed by the primary output, then use this direct |
| 1214 | // output by by-passing the primary mix if possible, otherwise fall-through to primary |
| 1215 | // mix. |
| 1216 | bool tryDirectForChannelMask = policyDesc != nullptr |
| 1217 | && (audio_channel_count_from_out_mask(policyDesc->getConfig().channel_mask) < |
| 1218 | audio_channel_count_from_out_mask(config->channel_mask)); |
| 1219 | if (deviceDesc != nullptr && (tryDirectForFlags || tryDirectForChannelMask)) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1220 | audio_io_handle_t newOutput; |
| 1221 | status = openDirectOutput( |
| 1222 | *stream, session, config, |
| 1223 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
| 1224 | DeviceVector(deviceDesc), &newOutput); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1225 | if (status == NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1226 | policyDesc = mOutputs.valueFor(newOutput); |
| 1227 | primaryMix->setOutput(policyDesc); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1228 | } else if (tryDirectForFlags) { |
| 1229 | policyDesc = nullptr; |
| 1230 | } // otherwise use primary if available. |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1231 | } |
| 1232 | if (policyDesc != nullptr) { |
| 1233 | policyDesc->mPolicyMix = primaryMix; |
| 1234 | *output = policyDesc->mIoHandle; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1235 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1236 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1237 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1238 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1239 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1240 | } else { |
| 1241 | *outputType = API_OUTPUT_LEGACY; |
| 1242 | } |
| 1243 | return NO_ERROR; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1244 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1245 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1246 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1247 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1248 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1249 | return BAD_VALUE; |
| 1250 | } |
| 1251 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1252 | // in order to let the choice of the order to future vendor engine |
| 1253 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1254 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1255 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1256 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1259 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1260 | // chosen by the engine if not. |
| 1261 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1262 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1263 | // 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] | 1264 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1265 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1266 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1267 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1268 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1269 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1270 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1274 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1275 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1276 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1277 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1278 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1279 | if (!msdDevices.isEmpty()) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1280 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags, isSpatialized); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1281 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1282 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1283 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1284 | } else { |
| 1285 | *output = AUDIO_IO_HANDLE_NONE; |
| 1286 | } |
| 1287 | } |
| 1288 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1289 | sp<PreferredMixerAttributesInfo> info = nullptr; |
| 1290 | if (outputDevices.size() == 1) { |
| 1291 | info = getPreferredMixerAttributesInfo( |
| 1292 | outputDevices.itemAt(0)->getId(), |
| 1293 | mEngine->getProductStrategyForAttributes(*resultAttr)); |
| 1294 | if (info != nullptr && info->getUid() != uid && info->getActiveClientCount() == 0) { |
| 1295 | // Only use preferred mixer when the requested uid matched or |
| 1296 | // there is active client on preferred mixer. |
| 1297 | info = nullptr; |
| 1298 | } |
| 1299 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1300 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1301 | flags, isSpatialized, info, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1302 | *isBitPerfect = (info != nullptr |
| 1303 | && (info->getFlags() & AUDIO_OUTPUT_FLAG_BIT_PERFECT) != AUDIO_OUTPUT_FLAG_NONE |
| 1304 | && *output != AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1305 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1306 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1307 | AudioProfileVector profiles; |
| 1308 | status_t ret = getProfilesForDevices(outputDevices, profiles, *flags, false /*isInput*/); |
| 1309 | if (ret == NO_ERROR && !profiles.empty()) { |
| 1310 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 1311 | : *profiles[0]->getChannels().begin(); |
| 1312 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 1313 | : *profiles[0]->getSampleRates().begin(); |
| 1314 | config->format = profiles[0]->getFormat(); |
| 1315 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1316 | return INVALID_OPERATION; |
| 1317 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1318 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1319 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1320 | for (auto &outputDevice : outputDevices) { |
| 1321 | if (outputDevice->getId() == getConfig().getDefaultOutputDevice()->getId()) { |
| 1322 | *selectedDeviceId = outputDevice->getId(); |
| 1323 | break; |
| 1324 | } |
| 1325 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1326 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1327 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1328 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1329 | } else { |
| 1330 | *outputType = API_OUTPUT_LEGACY; |
| 1331 | } |
| 1332 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1333 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1334 | |
| 1335 | return NO_ERROR; |
| 1336 | } |
| 1337 | |
| 1338 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1339 | audio_io_handle_t *output, |
| 1340 | audio_session_t session, |
| 1341 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1342 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1343 | audio_config_t *config, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1344 | audio_output_flags_t *flags, |
| 1345 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1346 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1347 | std::vector<audio_io_handle_t> *secondaryOutputs, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1348 | output_type_t *outputType, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1349 | bool *isSpatialized, |
| 1350 | bool *isBitPerfect) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1351 | { |
| 1352 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1353 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1354 | return INVALID_OPERATION; |
| 1355 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1356 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1357 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1358 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1359 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1360 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1361 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1362 | const sp<DeviceDescriptor> requestedDevice = |
| 1363 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1364 | |
| 1365 | // Prevent from storing invalid requested device id in clients |
| 1366 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1367 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1368 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1369 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1370 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1371 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 1372 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType, isSpatialized, |
| 1373 | isBitPerfect); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1374 | if (status != NO_ERROR) { |
| 1375 | return status; |
| 1376 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1377 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1378 | if (secondaryOutputs != nullptr) { |
| 1379 | for (auto &secondaryMix : secondaryMixes) { |
| 1380 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1381 | if (outputDesc != nullptr && |
| 1382 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1383 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1384 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1385 | } |
| 1386 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1387 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1388 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1389 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1390 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1391 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1392 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1393 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1394 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1395 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1396 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1397 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1398 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1399 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1400 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1401 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1402 | std::move(weakSecondaryOutputDescs), |
| 1403 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1404 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1405 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1406 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1407 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1408 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1409 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1412 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1413 | audio_session_t session, |
| 1414 | const audio_config_t *config, |
| 1415 | audio_output_flags_t flags, |
| 1416 | const DeviceVector &devices, |
| 1417 | audio_io_handle_t *output) { |
| 1418 | |
| 1419 | *output = AUDIO_IO_HANDLE_NONE; |
| 1420 | |
| 1421 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1422 | // and not explicitly requested |
| 1423 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1424 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1425 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1426 | return NAME_NOT_FOUND; |
| 1427 | } |
| 1428 | |
| 1429 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1430 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1431 | // when audioflinger detects there is an active non offloadable effect. |
| 1432 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1433 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1434 | // in the background. |
| 1435 | sp<IOProfile> profile; |
| 1436 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1437 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1438 | profile = getProfileForOutput( |
| 1439 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1440 | flags, true /* directOnly */); |
| 1441 | } |
| 1442 | |
| 1443 | if (profile == nullptr) { |
| 1444 | return NAME_NOT_FOUND; |
| 1445 | } |
| 1446 | |
| 1447 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1448 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1449 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1450 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1451 | // reuse direct output if currently open by the same client |
| 1452 | // and configured with same parameters |
| 1453 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1454 | (config->format == desc->getFormat()) && |
| 1455 | (config->channel_mask == desc->getChannelMask()) && |
| 1456 | (session == desc->mDirectClientSession)) { |
| 1457 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1458 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1459 | mOutputs.keyAt(i), session); |
| 1460 | *output = mOutputs.keyAt(i); |
| 1461 | return NO_ERROR; |
| 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | if (!profile->canOpenNewIo()) { |
| 1467 | return NAME_NOT_FOUND; |
| 1468 | } |
| 1469 | |
| 1470 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1471 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 1472 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1473 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1474 | // 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] | 1475 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1476 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1477 | status_t status = |
| 1478 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, flags, output); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1479 | |
| 1480 | // only accept an output with the requested parameters |
| 1481 | if (status != NO_ERROR || |
| 1482 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1483 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1484 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1485 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1486 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1487 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1488 | config->channel_mask, outputDesc->getChannelMask()); |
| 1489 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1490 | outputDesc->close(); |
| 1491 | } |
| 1492 | // fall back to mixer output if possible when the direct output could not be open |
| 1493 | if (audio_is_linear_pcm(config->format) && |
| 1494 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1495 | return NAME_NOT_FOUND; |
| 1496 | } |
| 1497 | *output = AUDIO_IO_HANDLE_NONE; |
| 1498 | return BAD_VALUE; |
| 1499 | } |
| 1500 | outputDesc->mDirectOpenCount = 1; |
| 1501 | outputDesc->mDirectClientSession = session; |
| 1502 | |
| 1503 | addOutput(*output, outputDesc); |
| 1504 | mPreviousOutputs = mOutputs; |
| 1505 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1506 | mpClientInterface->onAudioPortListUpdate(); |
| 1507 | return NO_ERROR; |
| 1508 | } |
| 1509 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1510 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1511 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1512 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1513 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1514 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1515 | audio_output_flags_t *flags, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1516 | bool *isSpatialized, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1517 | sp<PreferredMixerAttributesInfo> prefMixerConfigInfo, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1518 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1519 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1520 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1521 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1522 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1523 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1524 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1525 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1526 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1527 | // open a direct output if required by specified parameters |
| 1528 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1529 | // and all common behaviors are driven by checking only the direct flag |
| 1530 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1531 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1532 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1533 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1534 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1535 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1536 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1537 | |
| 1538 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1539 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1540 | // only allow deep buffering for music stream type |
| 1541 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1542 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1543 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1544 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1545 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1546 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1547 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1548 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1549 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1550 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1551 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1552 | audio_is_linear_pcm(config->format) && |
| 1553 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1554 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1555 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1556 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1557 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1558 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1559 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1560 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1561 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1562 | } |
| 1563 | |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1564 | *isSpatialized = false; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1565 | if (mSpatializerOutput != nullptr |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 1566 | && canBeSpatializedInt(attr, config, devices.toTypeAddrVector())) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1567 | *isSpatialized = true; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1568 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1571 | audio_config_t directConfig = *config; |
| 1572 | directConfig.channel_mask = channelMask; |
| 1573 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1574 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1575 | return output; |
| 1576 | } |
| 1577 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1578 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1579 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1580 | 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] | 1581 | return AUDIO_IO_HANDLE_NONE; |
| 1582 | } |
| 1583 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1584 | // ignoring channel mask due to downmix capability in mixer |
| 1585 | |
| 1586 | // open a non direct output |
| 1587 | |
| 1588 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1589 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1590 | // get which output is suitable for the specified stream. The actual |
| 1591 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1592 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 1593 | if (prefMixerConfigInfo != nullptr) { |
| 1594 | for (audio_io_handle_t outputHandle : outputs) { |
| 1595 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputHandle); |
| 1596 | if (outputDesc->mProfile == prefMixerConfigInfo->getProfile()) { |
| 1597 | output = outputHandle; |
| 1598 | break; |
| 1599 | } |
| 1600 | } |
| 1601 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 1602 | // No output open with the preferred profile. Open a new one. |
| 1603 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1604 | config.channel_mask = prefMixerConfigInfo->getConfigBase().channel_mask; |
| 1605 | config.sample_rate = prefMixerConfigInfo->getConfigBase().sample_rate; |
| 1606 | config.format = prefMixerConfigInfo->getConfigBase().format; |
| 1607 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 1608 | prefMixerConfigInfo->getProfile(), devices, nullptr /*mixerConfig*/, |
| 1609 | &config, prefMixerConfigInfo->getFlags()); |
| 1610 | if (preferredOutput == nullptr) { |
| 1611 | ALOGE("%s failed to open output with preferred mixer config", __func__); |
| 1612 | } else { |
| 1613 | output = preferredOutput->mIoHandle; |
| 1614 | } |
| 1615 | } |
| 1616 | } else { |
| 1617 | // at this stage we should ignore the DIRECT flag as no direct output could be |
| 1618 | // found earlier |
| 1619 | *flags = (audio_output_flags_t) (*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1620 | output = selectOutput( |
| 1621 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
| 1622 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1623 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1624 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1625 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1626 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1627 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1628 | return output; |
| 1629 | } |
| 1630 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1631 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1632 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1633 | mAvailableInputDevices); |
| 1634 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1635 | } |
| 1636 | |
| 1637 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1638 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1639 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1640 | } |
| 1641 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1642 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1643 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1644 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1645 | if (msdModule != 0) { |
| 1646 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1647 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1648 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1649 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1650 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1651 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1652 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1653 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | return msdPatches; |
| 1658 | } |
| 1659 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1660 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1661 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1662 | if (index < 0) { |
| 1663 | return false; |
| 1664 | } |
| 1665 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1666 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1667 | if (msdModule == nullptr) { |
| 1668 | return false; |
| 1669 | } |
| 1670 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1671 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1672 | return true; |
| 1673 | } |
| 1674 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1675 | if (index < 0) { |
| 1676 | return false; |
| 1677 | } |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1681 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1682 | const InputProfileCollection &inputProfiles, |
| 1683 | const OutputProfileCollection &outputProfiles, |
| 1684 | const sp<DeviceDescriptor> &sourceDevice, |
| 1685 | const sp<DeviceDescriptor> &sinkDevice, |
| 1686 | AudioProfileVector& sourceProfiles, |
| 1687 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1688 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1689 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1690 | return NO_INIT; |
| 1691 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1692 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1693 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1694 | return NO_INIT; |
| 1695 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1696 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1697 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1698 | inProfile->supportsDevice(sourceDevice)) { |
| 1699 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1700 | } |
| 1701 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1702 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1703 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1704 | outProfile->supportsDevice(sinkDevice)) { |
| 1705 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1706 | } |
| 1707 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1708 | return NO_ERROR; |
| 1709 | } |
| 1710 | |
| 1711 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1712 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1713 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1714 | { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1715 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1716 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, |
| 1717 | msdCompressedFormatsOrder, msdSurroundChannelMasksOrder, |
| 1718 | true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1719 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1720 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1721 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1722 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1723 | } |
| 1724 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1725 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1726 | sinkConfig->format = bestSinkConfig.format; |
| 1727 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1728 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1729 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1730 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1731 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1732 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1733 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1734 | // raw and IEC61937 framed streams. |
| 1735 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1736 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1737 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1738 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1739 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1740 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1741 | sourceConfig->format = bestSinkConfig.format; |
| 1742 | // Copy input stream directly without any processing (e.g. resampling). |
| 1743 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1744 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1745 | if (hwAvSync) { |
| 1746 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1747 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1748 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1749 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1750 | } |
| 1751 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1752 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1753 | sinkConfig->config_mask |= config_mask; |
| 1754 | sourceConfig->config_mask |= config_mask; |
| 1755 | return NO_ERROR; |
| 1756 | } |
| 1757 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1758 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 1759 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1760 | { |
| 1761 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1762 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1763 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 1764 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 1765 | if (deviceModule == nullptr) { |
| 1766 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 1767 | return patchBuilder; |
| 1768 | } |
| 1769 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 1770 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 1771 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 1772 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 1773 | |
| 1774 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 1775 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 1776 | device : getMsdAudioOutDevices().itemAt(0); |
| 1777 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 1778 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1779 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1780 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1781 | AudioProfileVector sourceProfiles; |
| 1782 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1783 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1784 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1785 | for (auto hwAvSync : { true, false }) { |
| 1786 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 1787 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 1788 | continue; |
| 1789 | } |
| 1790 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 1791 | &sinkConfig) == NO_ERROR) { |
| 1792 | // Found a matching config. Re-create PatchBuilder with this config. |
| 1793 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1794 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1795 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1796 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1797 | " supporting PCM format conversion.", __func__); |
| 1798 | return patchBuilder; |
| 1799 | } |
| 1800 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1801 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1802 | DeviceVector devices; |
| 1803 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1804 | devices.add(*outputDevices); |
| 1805 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1806 | // Use media strategy for unspecified output device. This should only |
| 1807 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1808 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1809 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1810 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1811 | 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] | 1812 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1813 | std::vector<PatchBuilder> patchesToCreate; |
| 1814 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1815 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1816 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1817 | } |
| 1818 | // Retain only the MSD patches associated with outputDevices request. |
| 1819 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1820 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1821 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1822 | auto retainedPatch = false; |
| 1823 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1824 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1825 | patchesToRemove.removeItemsAt(i); |
| 1826 | retainedPatch = true; |
| 1827 | break; |
| 1828 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1829 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1830 | if (retainedPatch) { |
| 1831 | it = patchesToCreate.erase(it); |
| 1832 | continue; |
| 1833 | } |
| 1834 | ++it; |
| 1835 | } |
| 1836 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1837 | return NO_ERROR; |
| 1838 | } |
| 1839 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1840 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1841 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1842 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1843 | status_t status = NO_ERROR; |
| 1844 | for (const auto &p : patchesToCreate) { |
| 1845 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1846 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1847 | char message[256]; |
| 1848 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1849 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1850 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1851 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1852 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1853 | if (currStatus == NO_ERROR) { |
| 1854 | ALOGD("%s", message); |
| 1855 | } else { |
| 1856 | ALOGE("%s", message); |
| 1857 | if (status == NO_ERROR) { |
| 1858 | status = currStatus; |
| 1859 | } |
| 1860 | } |
| 1861 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1862 | return status; |
| 1863 | } |
| 1864 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1865 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 1866 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1867 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1868 | const auto& patch = msdPatches[i]; |
| 1869 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1870 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1871 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1872 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1873 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1874 | break; |
| 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | } |
| 1879 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1880 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
| 1881 | DeviceVector devicesToCheck = mOutputDevicesAll.getDevicesFromDeviceTypeAddrVec(devices); |
| 1882 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 1883 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1884 | const auto& patch = msdPatches[i]; |
| 1885 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1886 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1887 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1888 | const auto& foundDevice = devicesToCheck.getDevice( |
| 1889 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 1890 | if (foundDevice != nullptr) { |
| 1891 | devicesToCheck.remove(foundDevice); |
| 1892 | if (devicesToCheck.isEmpty()) { |
| 1893 | return true; |
| 1894 | } |
| 1895 | } |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | return false; |
| 1900 | } |
| 1901 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1902 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1903 | audio_output_flags_t flags, |
| 1904 | audio_format_t format, |
| 1905 | audio_channel_mask_t channelMask, |
| 1906 | uint32_t samplingRate, |
| 1907 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1908 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1909 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 1910 | "%s called with format %#x", __func__, format); |
| 1911 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1912 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 1913 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 1914 | // haptic-generating effect attached to is in given outputs. |
| 1915 | if (sessionId != AUDIO_SESSION_NONE) { |
| 1916 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 1917 | sessionId, FX_IID_HAPTICGENERATOR); |
| 1918 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 1919 | return hapticGeneratingOutput; |
| 1920 | } |
| 1921 | } |
| 1922 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1923 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 1924 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 1925 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1926 | |
| 1927 | // Flags expressing a functional request: must be honored in priority over |
| 1928 | // other criteria |
| 1929 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 1930 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 1931 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 1932 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1933 | // Flags expressing a performance request: have lower priority than serving |
| 1934 | // requested sampling rate or channel mask |
| 1935 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 1936 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 1937 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 1938 | |
| 1939 | const audio_output_flags_t functionalFlags = |
| 1940 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 1941 | const audio_output_flags_t performanceFlags = |
| 1942 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 1943 | |
| 1944 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 1945 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1946 | // 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] | 1947 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1948 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1949 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1950 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1951 | // with tiebreak preferring the minimum number of extra functional flags |
| 1952 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1953 | // 3: the output supporting the exact channel mask |
| 1954 | // 4: the output with a higher channel count than requested |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 1955 | // 5: the output with the highest sampling rate if the requested sample rate is |
| 1956 | // greater than default sampling rate |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1957 | // 6: the output with the highest number of requested performance flags |
| 1958 | // 7: the output with the bit depth the closest to the requested one |
| 1959 | // 8: the primary output |
| 1960 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1961 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1962 | // matching criteria values in priority order for best matching output so far |
| 1963 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1964 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1965 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 1966 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1967 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1968 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1969 | for (audio_io_handle_t output : outputs) { |
| 1970 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1971 | // matching criteria values in priority order for current output |
| 1972 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1973 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1974 | if (outputDesc->isDuplicated()) { |
| 1975 | continue; |
| 1976 | } |
| 1977 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 1978 | continue; |
| 1979 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1980 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1981 | // If haptic channel is specified, use the haptic output if present. |
| 1982 | // When using haptic output, same audio format and sample rate are required. |
| 1983 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1984 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1985 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 1986 | continue; |
| 1987 | } |
| 1988 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1989 | && format == outputDesc->getFormat() |
| 1990 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1991 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 1992 | } |
| 1993 | |
| 1994 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1995 | const int matchingFunctionalFlags = |
| 1996 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 1997 | const int totalFunctionalFlags = |
| 1998 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 1999 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 2000 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2001 | |
| 2002 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2003 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 2004 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2005 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 2006 | channelCount <= outputChannelCount) { |
| 2007 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2008 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 2009 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2010 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2011 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2012 | currentMatchCriteria[3] = outputChannelCount; |
| 2013 | } |
| 2014 | |
| 2015 | // sampling rate match |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 2016 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 2017 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | // performance flags match |
| 2021 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 2022 | |
| 2023 | // format match |
| 2024 | if (format != AUDIO_FORMAT_INVALID) { |
| 2025 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2026 | PolicyAudioPort::kFormatDistanceMax - |
| 2027 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | // primary output match |
| 2031 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 2032 | |
| 2033 | // compare match criteria by priority then value |
| 2034 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2035 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 2036 | bestMatchCriteria = currentMatchCriteria; |
| 2037 | bestOutput = output; |
| 2038 | |
| 2039 | std::stringstream result; |
| 2040 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 2041 | std::ostream_iterator<int>(result, " ")); |
| 2042 | ALOGV("%s new bestOutput %d criteria %s", |
| 2043 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2044 | } |
| 2045 | } |
| 2046 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 2047 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2050 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2051 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2052 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2053 | |
| 2054 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2055 | if (outputDesc == 0) { |
| 2056 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2057 | return BAD_VALUE; |
| 2058 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2059 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2060 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2061 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2062 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2063 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2064 | status_t status = outputDesc->start(); |
| 2065 | if (status != NO_ERROR) { |
| 2066 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2069 | uint32_t delayMs; |
| 2070 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2071 | |
| 2072 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2073 | outputDesc->stop(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2074 | if (status == DEAD_OBJECT) { |
| 2075 | sp<SwAudioOutputDescriptor> desc = |
| 2076 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2077 | if (desc == nullptr) { |
| 2078 | // This is not common, it may indicate something wrong with the HAL. |
| 2079 | ALOGE("%s unable to open output with default config", __func__); |
| 2080 | return status; |
| 2081 | } |
| 2082 | desc->mUsePreferredMixerAttributes = true; |
| 2083 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2084 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2085 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2086 | |
| 2087 | // If the client is the first one active on preferred mixer parameters, reopen the output |
| 2088 | // if the current mixer parameters doesn't match the preferred one. |
| 2089 | if (outputDesc->devices().size() == 1) { |
| 2090 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2091 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2092 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2093 | if (info->getActiveClientCount() == 0 && !outputDesc->isConfigurationMatched( |
| 2094 | info->getConfigBase(), info->getFlags())) { |
| 2095 | stopSource(outputDesc, client); |
| 2096 | outputDesc->stop(); |
| 2097 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2098 | config.channel_mask = info->getConfigBase().channel_mask; |
| 2099 | config.sample_rate = info->getConfigBase().sample_rate; |
| 2100 | config.format = info->getConfigBase().format; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2101 | sp<SwAudioOutputDescriptor> desc = |
| 2102 | reopenOutput(outputDesc, &config, info->getFlags(), __func__); |
| 2103 | if (desc == nullptr) { |
| 2104 | return BAD_VALUE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2105 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2106 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2107 | // Intentionally return error to let the client side resending request for |
| 2108 | // creating and starting. |
| 2109 | return DEAD_OBJECT; |
| 2110 | } |
| 2111 | info->increaseActiveClient(); |
| 2112 | } |
| 2113 | } |
| 2114 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2115 | if (delayMs != 0) { |
| 2116 | usleep(delayMs * 1000); |
| 2117 | } |
| 2118 | |
| 2119 | return status; |
| 2120 | } |
| 2121 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2122 | bool AudioPolicyManager::isLeUnicastActive() const { |
| 2123 | if (isInCall()) { |
| 2124 | return true; |
| 2125 | } |
| 2126 | return isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet()); |
| 2127 | } |
| 2128 | |
| 2129 | bool AudioPolicyManager::isAnyDeviceTypeActive(const DeviceTypeSet& deviceTypes) const { |
| 2130 | if (mAvailableOutputDevices.getDevicesFromTypes(deviceTypes).isEmpty()) { |
| 2131 | return false; |
| 2132 | } |
| 2133 | bool active = mOutputs.isAnyDeviceTypeActive(deviceTypes); |
| 2134 | ALOGV("%s active %d", __func__, active); |
| 2135 | return active; |
| 2136 | } |
| 2137 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2138 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2139 | const sp<TrackClientDescriptor>& client, |
| 2140 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2141 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2142 | // cannot start playback of STREAM_TTS if any other output is being used |
| 2143 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2144 | |
| 2145 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2146 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2147 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2148 | auto clientStrategy = client->strategy(); |
| 2149 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2150 | if (stream == AUDIO_STREAM_TTS) { |
| 2151 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2152 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 2153 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2154 | return INVALID_OPERATION; |
| 2155 | } else { |
| 2156 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2157 | } |
| 2158 | } else { |
| 2159 | // some playback other than beacon starts |
| 2160 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2161 | } |
| 2162 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2163 | // 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] | 2164 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2165 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2166 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2167 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2168 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2169 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2170 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2171 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2172 | address = policyMix->mDeviceAddress.string(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2173 | 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] | 2174 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2175 | } else { |
| 2176 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2177 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2178 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2179 | AUDIO_FORMAT_DEFAULT); |
| 2180 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2181 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2184 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2185 | // It covers a common case when there is no materially active audio |
| 2186 | // and muting would result in unnecessary delay and dropped audio. |
| 2187 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2188 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2189 | bool wasLeUnicastActive = isLeUnicastActive(); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2190 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2191 | // increment usage count for this stream on the requested output: |
| 2192 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2193 | // 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] | 2194 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2195 | |
| 2196 | if (client->hasPreferredDevice(true)) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2197 | if (outputDesc->clientsList(true /*activeOnly*/).size() == 1 && |
| 2198 | client->isPreferredDeviceForExclusiveUse()) { |
| 2199 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2200 | devices = DeviceVector( |
| 2201 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2202 | } else { |
| 2203 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2204 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2205 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2206 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2207 | } |
| 2208 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2209 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2210 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2211 | selectOutputForMusicEffects(); |
| 2212 | } |
| 2213 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2214 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2215 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2216 | if (devices.isEmpty()) { |
| 2217 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2218 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2219 | bool shouldWait = |
| 2220 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2221 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2222 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2223 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2224 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2225 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2226 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2227 | // An output has a shared device if |
| 2228 | // - managed by the same hw module |
| 2229 | // - supports the currently selected device |
| 2230 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2231 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2232 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2233 | // force a device change if any other output is: |
| 2234 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2235 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2236 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2237 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2238 | // 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] | 2239 | // change the device currently selected by the other output. |
| 2240 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2241 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2242 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2243 | force = true; |
| 2244 | } |
| 2245 | // 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] | 2246 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2247 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2248 | const uint32_t latencyMs = desc->latency(); |
| 2249 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2250 | |
| 2251 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2252 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2253 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2254 | |
| 2255 | // Require mute check if another output is on a shared device |
| 2256 | // and currently active to have proper drain and avoid pops. |
| 2257 | // Note restoring AudioTracks onto this output needs to invoke |
| 2258 | // a volume ramp if there is no mute. |
| 2259 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2260 | } |
| 2261 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2262 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2263 | if (outputDesc->mUsePreferredMixerAttributes && devices != outputDesc->devices()) { |
| 2264 | // If the output is open with preferred mixer attributes, but the routed device is |
| 2265 | // changed when calling this function, returning DEAD_OBJECT to indicate routing |
| 2266 | // changed. |
| 2267 | return DEAD_OBJECT; |
| 2268 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2269 | const uint32_t muteWaitMs = |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2270 | setOutputDevices(outputDesc, devices, force, 0, nullptr, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2271 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2272 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2273 | auto &curves = getVolumeCurves(client->attributes()); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2274 | if (NO_ERROR != checkAndSetVolume(curves, client->volumeSource(), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2275 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2276 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2277 | outputDesc->devices().types(), 0 /*delay*/, |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2278 | outputDesc->useHwGain() /*force*/)) { |
| 2279 | // request AudioService to reinitialize the volume curves asynchronously |
| 2280 | ALOGE("checkAndSetVolume failed, requesting volume range init"); |
| 2281 | mpClientInterface->onVolumeRangeInitRequest(); |
| 2282 | }; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2283 | |
| 2284 | // update the outputs if starting an output with a stream that can affect notification |
| 2285 | // routing |
| 2286 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2287 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2288 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2289 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2290 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 2291 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2292 | |
| 2293 | if (waitMs > muteWaitMs) { |
| 2294 | *delayMs = waitMs - muteWaitMs; |
| 2295 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2296 | |
| 2297 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2298 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2299 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2300 | // change occurs after the MixerThread starts and causes a stream volume |
| 2301 | // glitch. |
| 2302 | // |
| 2303 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2304 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2305 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2306 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2307 | mEngine->getForceUse( |
| 2308 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2309 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2310 | } |
| 2311 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2312 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2313 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2314 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2315 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2316 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2317 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2318 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2319 | "remote-submix", |
| 2320 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2323 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, *delayMs); |
| 2324 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2325 | return NO_ERROR; |
| 2326 | } |
| 2327 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2328 | void AudioPolicyManager::checkLeBroadcastRoutes(bool wasUnicastActive, |
| 2329 | sp<SwAudioOutputDescriptor> ignoredOutput, uint32_t delayMs) { |
| 2330 | bool isUnicastActive = isLeUnicastActive(); |
| 2331 | |
| 2332 | if (wasUnicastActive != isUnicastActive) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2333 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2334 | //reroute all outputs routed to LE broadcast if LE unicast activy changed on any output |
| 2335 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2336 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2337 | if (desc != ignoredOutput && desc->isActive() |
| 2338 | && ((isUnicastActive && |
| 2339 | !desc->devices(). |
| 2340 | getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST).isEmpty()) |
| 2341 | || (wasUnicastActive && |
| 2342 | !desc->devices().getDevicesFromTypes( |
| 2343 | getAudioDeviceOutLeAudioUnicastSet()).isEmpty()))) { |
| 2344 | DeviceVector newDevices = getNewOutputDevices(desc, false /*fromCache*/); |
| 2345 | bool force = desc->devices() != newDevices; |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2346 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2347 | // If the device is using preferred mixer attributes, the output need to reopen |
| 2348 | // with default configuration when the new selected devices are different from |
| 2349 | // current routing devices. |
| 2350 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 2351 | continue; |
| 2352 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2353 | setOutputDevices(desc, newDevices, force, delayMs); |
| 2354 | // re-apply device specific volume if not done by setOutputDevice() |
| 2355 | if (!force) { |
| 2356 | applyStreamVolumes(desc, newDevices.types(), delayMs); |
| 2357 | } |
| 2358 | } |
| 2359 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2360 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2364 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2365 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2366 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2367 | |
| 2368 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2369 | if (outputDesc == 0) { |
| 2370 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2371 | return BAD_VALUE; |
| 2372 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2373 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2374 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2375 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2376 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2377 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2378 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2379 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2380 | if (status == NO_ERROR ) { |
| 2381 | outputDesc->stop(); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 2382 | } else { |
| 2383 | return status; |
| 2384 | } |
| 2385 | |
| 2386 | if (outputDesc->devices().size() == 1) { |
| 2387 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 2388 | outputDesc->devices()[0]->getId(), client->strategy()); |
| 2389 | if (info != nullptr && info->getUid() == client->uid()) { |
| 2390 | info->decreaseActiveClient(); |
| 2391 | if (info->getActiveClientCount() == 0) { |
| 2392 | reopenOutput(outputDesc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 2393 | } |
| 2394 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2395 | } |
| 2396 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2399 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2400 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2401 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2402 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2403 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2404 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2405 | bool wasLeUnicastActive = isLeUnicastActive(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2406 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2407 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2408 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2409 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2410 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2411 | // Automatically disable the remote submix input when output is stopped on a |
| 2412 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2413 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2414 | if (isSingleDeviceType( |
| 2415 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2416 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2417 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2418 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2419 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2420 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2421 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | bool forceDeviceUpdate = false; |
| 2425 | if (client->hasPreferredDevice(true)) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2426 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2427 | forceDeviceUpdate = true; |
| 2428 | } |
| 2429 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2430 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2431 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2432 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2433 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2434 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2435 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2436 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2437 | |
| 2438 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2439 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2440 | // volume group(s), force reapply volume |
| 2441 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2442 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2443 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2444 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2445 | // the track stop() command is received and at that time the audio track buffer can |
| 2446 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2447 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2448 | // audio path (audio DSP, CODEC ...) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2449 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2, |
| 2450 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2451 | |
| 2452 | // force restoring the device selection on other active outputs if it differs from the |
| 2453 | // one being selected for this output |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2454 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2455 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2456 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2457 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2458 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2459 | desc->isActive() && |
| 2460 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2461 | (newDevices != desc->devices())) { |
| 2462 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2463 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2464 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2465 | if (desc->mUsePreferredMixerAttributes && force) { |
| 2466 | // If the device is using preferred mixer attributes, the output need to |
| 2467 | // reopen with default configuration when the new selected devices are |
| 2468 | // different from current routing devices. |
| 2469 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices2); |
| 2470 | continue; |
| 2471 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2472 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 2473 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2474 | // re-apply device specific volume if not done by setOutputDevice() |
| 2475 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2476 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2477 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2478 | } |
| 2479 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 2480 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2481 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2482 | handleNotificationRoutingForStream(stream); |
| 2483 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2484 | |
| 2485 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2486 | 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] | 2487 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2488 | } |
| 2489 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2490 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2491 | selectOutputForMusicEffects(); |
| 2492 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2493 | |
| 2494 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, outputDesc->latency()*2); |
| 2495 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2496 | return NO_ERROR; |
| 2497 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2498 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2499 | return INVALID_OPERATION; |
| 2500 | } |
| 2501 | } |
| 2502 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2503 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2504 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2505 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2506 | |
| 2507 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2508 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2509 | // If an output descriptor is closed due to a device routing change, |
| 2510 | // then there are race conditions with releaseOutput from tracks |
| 2511 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2512 | // destroyed shortly thereafter. |
| 2513 | // |
| 2514 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2515 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2516 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2517 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2518 | |
| 2519 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2520 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2521 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2522 | if (outputDesc->isClientActive(client)) { |
| 2523 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2524 | stopOutput(portId); |
| 2525 | } |
| 2526 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2527 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2528 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2529 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2530 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2531 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2532 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2533 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2534 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2535 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2536 | } |
| 2537 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2538 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2539 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2540 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2541 | // The output is pending reopened to query dynamic profiles and |
| 2542 | // there is no active clients |
| 2543 | closeOutput(outputDesc->mIoHandle); |
| 2544 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2545 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2546 | if (newOutputDesc == nullptr) { |
| 2547 | ALOGE("%s failed to open output", __func__); |
| 2548 | } |
| 2549 | return true; |
| 2550 | } |
| 2551 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2554 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2555 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2556 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2557 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2558 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2559 | audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2560 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2561 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2562 | input_type_t *inputType, |
| 2563 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2564 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2565 | 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] | 2566 | "flags %#x attributes=%s requested device ID %d", |
| 2567 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2568 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2569 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2570 | status_t status = NO_ERROR; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2571 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2572 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2573 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2574 | sp<AudioInputDescriptor> inputDesc; |
| 2575 | sp<RecordClientDescriptor> clientDesc; |
| 2576 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2577 | 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] | 2578 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2579 | |
| 2580 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2581 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2582 | return INVALID_OPERATION; |
| 2583 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2584 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2585 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2586 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2587 | } |
| 2588 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2589 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2590 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2591 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2592 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2593 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2594 | // possible |
| 2595 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2596 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2597 | ssize_t index = mInputs.indexOfKey(*input); |
| 2598 | if (index < 0) { |
| 2599 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2600 | status = BAD_VALUE; |
| 2601 | goto error; |
| 2602 | } |
| 2603 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2604 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2605 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2606 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2607 | status = BAD_VALUE; |
| 2608 | goto error; |
| 2609 | } |
| 2610 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2611 | // 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] | 2612 | // corresponds to a new client and is only permitted from the same UID. |
| 2613 | // 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] | 2614 | if (clients.size() > 1) { |
| 2615 | for (const auto& client : clients) { |
| 2616 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2617 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2618 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2619 | // to an actual client |
| 2620 | if (client == *clients.cbegin()) { |
| 2621 | continue; |
| 2622 | } |
| 2623 | if (uid != client->uid() && !client->isSilenced()) { |
| 2624 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2625 | uid, client->portId(), client->uid()); |
| 2626 | status = INVALID_OPERATION; |
| 2627 | goto error; |
| 2628 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2629 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2630 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2631 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2632 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2633 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2634 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2635 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | *input = AUDIO_IO_HANDLE_NONE; |
| 2639 | *inputType = API_INPUT_INVALID; |
| 2640 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2641 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
Jan Sebechlebsky | bc56bcd | 2022-09-26 13:15:19 +0200 | [diff] [blame] | 2642 | extractAddressFromAudioAttributes(attributes).has_value()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2643 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2644 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2645 | ALOGW("%s could not find input mix for attr %s", |
| 2646 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2647 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2648 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2649 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2650 | String8(attr->tags + strlen("addr=")), |
| 2651 | AUDIO_FORMAT_DEFAULT); |
| 2652 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2653 | 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] | 2654 | __func__, attributes.source, attributes.tags); |
| 2655 | status = BAD_VALUE; |
| 2656 | goto error; |
| 2657 | } |
| 2658 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2659 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2660 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2661 | } else { |
| 2662 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2663 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2664 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2665 | if (explicitRoutingDevice != nullptr) { |
| 2666 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2667 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2668 | // Prevent from storing invalid requested device id in clients |
| 2669 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 2670 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2671 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 2672 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2673 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2674 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2675 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2676 | status = BAD_VALUE; |
| 2677 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2678 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2679 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2680 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2681 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2682 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2683 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2684 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2685 | // know about it and is therefore considered "legacy" |
| 2686 | *inputType = API_INPUT_LEGACY; |
| 2687 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2688 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2689 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2690 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2691 | } else { |
| 2692 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2693 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2694 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2695 | } |
| 2696 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2697 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2698 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2699 | status = INVALID_OPERATION; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2700 | AudioProfileVector profiles; |
| 2701 | status_t ret = getProfilesForDevices( |
| 2702 | DeviceVector(device), profiles, flags, true /*isInput*/); |
| 2703 | if (ret == NO_ERROR && !profiles.empty()) { |
| 2704 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 2705 | : *profiles[0]->getChannels().begin(); |
| 2706 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 2707 | : *profiles[0]->getSampleRates().begin(); |
| 2708 | config->format = profiles[0]->getFormat(); |
| 2709 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2710 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2711 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2712 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2713 | exit: |
| 2714 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2715 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2716 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2717 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2718 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2719 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2720 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2721 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2722 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2723 | requestedDeviceId, attributes.source, flags, |
| 2724 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2725 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2726 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2727 | |
| 2728 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2729 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2730 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2731 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2732 | |
| 2733 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2734 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2738 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2739 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2740 | const audio_attributes_t &attributes, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2741 | audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2742 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2743 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2744 | { |
| 2745 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2746 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2747 | bool isSoundTrigger = false; |
| 2748 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2749 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2750 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2751 | if (index >= 0) { |
| 2752 | input = mSoundTriggerSessions.valueFor(session); |
| 2753 | isSoundTrigger = true; |
| 2754 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2755 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2756 | } else { |
| 2757 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2758 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2759 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2760 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2761 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2762 | } |
| 2763 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 2764 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 2765 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 2766 | } |
| 2767 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2768 | // sampling rate and flags may be updated by getInputProfile |
| 2769 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2770 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2771 | audio_format_t profileFormat = config->format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2772 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2773 | audio_input_flags_t profileFlags = flags; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2774 | // find a compatible input profile (not necessarily identical in parameters) |
| 2775 | sp<IOProfile> profile = getInputProfile( |
| 2776 | device, profileSamplingRate, profileFormat, profileChannelMask, profileFlags); |
| 2777 | if (profile == nullptr) { |
| 2778 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2779 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2780 | |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2781 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2782 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2783 | if (samplingRate == 0) { |
| 2784 | samplingRate = profileSamplingRate; |
| 2785 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2786 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2787 | if (profile->getModuleHandle() == 0) { |
| 2788 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2789 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2790 | } |
| 2791 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2792 | // Reuse an already opened input if a client with the same session ID already exists |
| 2793 | // on that input |
| 2794 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2795 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2796 | if (desc->mProfile != profile) { |
| 2797 | continue; |
| 2798 | } |
| 2799 | RecordClientVector clients = desc->clientsList(); |
| 2800 | for (const auto &client : clients) { |
| 2801 | if (session == client->session()) { |
| 2802 | return desc->mIoHandle; |
| 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2807 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2808 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2809 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2810 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2811 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2812 | continue; |
| 2813 | } |
| 2814 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2815 | // else |
| 2816 | // reuse input if active client app is not in IDLE state |
| 2817 | // |
| 2818 | RecordClientVector clients = desc->clientsList(); |
| 2819 | bool doClose = false; |
| 2820 | for (const auto& client : clients) { |
| 2821 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2822 | continue; |
| 2823 | } |
| 2824 | if (client->isSoundTrigger()) { |
| 2825 | if (session == client->session()) { |
| 2826 | return desc->mIoHandle; |
| 2827 | } |
| 2828 | continue; |
| 2829 | } |
| 2830 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2831 | return desc->mIoHandle; |
| 2832 | } |
| 2833 | doClose = true; |
| 2834 | } |
| 2835 | if (doClose) { |
| 2836 | closeInput(desc->mIoHandle); |
| 2837 | } else { |
| 2838 | i++; |
| 2839 | } |
| 2840 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2841 | } |
| 2842 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2843 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2844 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2845 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2846 | lConfig.sample_rate = profileSamplingRate; |
| 2847 | lConfig.channel_mask = profileChannelMask; |
| 2848 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2849 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2850 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2851 | |
| 2852 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2853 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2854 | (profileSamplingRate != lConfig.sample_rate) || |
| 2855 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2856 | (profileChannelMask != lConfig.channel_mask)) { |
| 2857 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2858 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2859 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2860 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2861 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2862 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2863 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2864 | } |
| 2865 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2866 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2867 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2868 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2869 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2870 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2871 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2874 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2875 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2876 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2877 | |
| 2878 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2879 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2880 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2881 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2882 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2883 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2884 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2885 | if (client->active()) { |
| 2886 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2887 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2890 | audio_session_t session = client->session(); |
| 2891 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2892 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2893 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2894 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2895 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2896 | status_t status = inputDesc->start(); |
| 2897 | if (status != NO_ERROR) { |
| 2898 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2899 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2900 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2901 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2902 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2903 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2904 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2905 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2906 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2907 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2908 | if (device != nullptr) { |
| 2909 | status = setInputDevice(input, device, true /* force */); |
| 2910 | } else { |
| 2911 | ALOGW("%s no new input device can be found for descriptor %d", |
| 2912 | __FUNCTION__, inputDesc->getId()); |
| 2913 | status = BAD_VALUE; |
| 2914 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2915 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2916 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2917 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2918 | // 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] | 2919 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2920 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2921 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2922 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2923 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2924 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2925 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2926 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2927 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2928 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2929 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2930 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2931 | // automatically enable the remote submix output when input is started if not |
| 2932 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2933 | // 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] | 2934 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2935 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2936 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2937 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2938 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2939 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2940 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2941 | if (address != "") { |
| 2942 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2943 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2944 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2945 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2946 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2947 | } else if (status != NO_ERROR) { |
| 2948 | // Restore client activity state. |
| 2949 | inputDesc->setClientActive(client, false); |
| 2950 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2953 | ALOGV("%s input %d source = %d status = %d exit", |
| 2954 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2955 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2956 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2957 | } |
| 2958 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2959 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2960 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2961 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2962 | |
| 2963 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2964 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2965 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2966 | return BAD_VALUE; |
| 2967 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2968 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2969 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2970 | if (!client->active()) { |
| 2971 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2972 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2973 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2974 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2975 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2976 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2977 | inputDesc->stop(); |
| 2978 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2979 | auto current_source = inputDesc->source(); |
| 2980 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 2981 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2982 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2983 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2984 | // 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] | 2985 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2986 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2987 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2988 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2989 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2990 | |
| 2991 | // automatically disable the remote submix output when input is stopped if not |
| 2992 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2993 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2994 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2995 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2996 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2997 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2998 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2999 | } |
| 3000 | if (address != "") { |
| 3001 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 3002 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3003 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3004 | } |
| 3005 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3006 | resetInputDevice(input); |
| 3007 | |
| 3008 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 3009 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3010 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 3011 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3012 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 3013 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3014 | } |
| 3015 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3016 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3017 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3018 | } |
| 3019 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3020 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3021 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3022 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 3023 | |
| 3024 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 3025 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3026 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3027 | return; |
| 3028 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3029 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3030 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 3031 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3032 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 3033 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3034 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3035 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3036 | if (inputDesc->getClientCount() > 0) { |
| 3037 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3038 | return; |
| 3039 | } |
| 3040 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3041 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3042 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3043 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3044 | } |
| 3045 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3046 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3047 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3048 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3049 | |
| 3050 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3051 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3055 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 3056 | { |
| 3057 | stopInput(portId); |
| 3058 | releaseInput(portId); |
| 3059 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 3060 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3061 | void AudioPolicyManager::checkCloseInputs() { |
| 3062 | // After connecting or disconnecting an input device, close input if: |
| 3063 | // - it has no client (was just opened to check profile) OR |
| 3064 | // - none of its supported devices are connected anymore OR |
| 3065 | // - one of its clients cannot be routed to one of its supported |
| 3066 | // devices anymore. Otherwise update device selection |
| 3067 | std::vector<audio_io_handle_t> inputsToClose; |
| 3068 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3069 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 3070 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 3071 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3072 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3073 | } else { |
| 3074 | bool close = false; |
| 3075 | for (const auto& client : input->clientsList()) { |
| 3076 | sp<DeviceDescriptor> device = |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 3077 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid(), |
| 3078 | client->session()); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 3079 | if (!input->supportedDevices().contains(device)) { |
| 3080 | close = true; |
| 3081 | break; |
| 3082 | } |
| 3083 | } |
| 3084 | if (close) { |
| 3085 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 3086 | } else { |
| 3087 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 3088 | } |
| 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | for (const audio_io_handle_t handle : inputsToClose) { |
| 3093 | ALOGV("%s closing input %d", __func__, handle); |
| 3094 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3095 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 3098 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3099 | { |
| 3100 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 3101 | if (indexMin < 0 || indexMax < 0) { |
| 3102 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 3103 | return; |
| 3104 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3105 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3106 | |
| 3107 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3108 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 3109 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3110 | continue; |
| 3111 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 3112 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3113 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3114 | } |
| 3115 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3116 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3117 | int index, |
| 3118 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3119 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3120 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 3121 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 3122 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 3123 | return NO_ERROR; |
| 3124 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3125 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 3126 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3127 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3128 | } |
| 3129 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3130 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3131 | int *index, |
| 3132 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3133 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3134 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3135 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3136 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3137 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3138 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 3139 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3140 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3141 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3144 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3145 | int index, |
| 3146 | audio_devices_t device) |
| 3147 | { |
| 3148 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3149 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 3150 | if (group == VOLUME_GROUP_NONE) { |
| 3151 | 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] | 3152 | return BAD_VALUE; |
| 3153 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3154 | 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] | 3155 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3156 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3157 | VolumeSource vs = toVolumeSource(group); |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3158 | // AUDIO_STREAM_BLUETOOTH_SCO is only used for volume control so we remap |
| 3159 | // to AUDIO_STREAM_VOICE_CALL to match with relevant playback activity |
| 3160 | VolumeSource activityVs = (vs == toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false)) ? |
| 3161 | toVolumeSource(AUDIO_STREAM_VOICE_CALL, false) : vs; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3162 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 3163 | |
| 3164 | status = setVolumeCurveIndex(index, device, curves); |
| 3165 | if (status != NO_ERROR) { |
| 3166 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 3167 | return status; |
| 3168 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3169 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3170 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3171 | auto curCurvAttrs = curves.getAttributes(); |
| 3172 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 3173 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3174 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3175 | } else if (!curves.getStreamTypes().empty()) { |
| 3176 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3177 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3178 | } else { |
| 3179 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 3180 | return BAD_VALUE; |
| 3181 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3182 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 3183 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3184 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3185 | // update volume on all outputs and streams matching the following: |
| 3186 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 3187 | // - The device (or devices) selected by the engine for this stream includes |
| 3188 | // the requested device |
| 3189 | // - For non default requested device, currently selected device on the output is either the |
| 3190 | // requested device or one of the devices selected by the engine for this stream |
| 3191 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 3192 | // no specific device volume value exists for currently selected device. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3193 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3194 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3195 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3196 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3197 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 3198 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 3199 | } |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3200 | |
| 3201 | if (!(desc->isActive(activityVs) || isInCallOrScreening())) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3202 | continue; |
| 3203 | } |
| 3204 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 3205 | curDevices.find(device) == curDevices.end()) { |
| 3206 | continue; |
| 3207 | } |
| 3208 | bool applyVolume = false; |
| 3209 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 3210 | curSrcDevices.insert(device); |
| 3211 | applyVolume = (curSrcDevices.find( |
| 3212 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); |
| 3213 | } else { |
| 3214 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 3215 | } |
| 3216 | if (!applyVolume) { |
| 3217 | continue; // next output |
| 3218 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3219 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 3220 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 3221 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3222 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3223 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 3224 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 3225 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3226 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 3227 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 3228 | false /*preferredDevice*/); |
| 3229 | if (activeClients.empty()) { |
| 3230 | continue; |
| 3231 | } |
| 3232 | bool isPreempted = false; |
| 3233 | bool isHigherPriority = productStrategy < strategy; |
| 3234 | for (const auto &client : activeClients) { |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 3235 | if (isHigherPriority && (client->volumeSource() != activityVs)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3236 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3237 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3238 | " higher priority source active:\n" |
| 3239 | " volumeGroup=%d attributes=%s) \n" |
| 3240 | " on output %zu, bailing out", __func__, productStrategy, |
| 3241 | group, group, toString(attributes).c_str(), |
| 3242 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3243 | applyVolume = false; |
| 3244 | isPreempted = true; |
| 3245 | break; |
| 3246 | } |
| 3247 | // 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] | 3248 | if (client->volumeSource() == activityVs) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3249 | applyVolume = true; |
| 3250 | } |
| 3251 | } |
| 3252 | if (isPreempted || applyVolume) { |
| 3253 | break; |
| 3254 | } |
| 3255 | } |
| 3256 | if (!applyVolume) { |
| 3257 | continue; // next output |
| 3258 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3259 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3260 | //FIXME: workaround for truncated touch sounds |
| 3261 | // delayed volume change for system stream to be removed when the problem is |
| 3262 | // handled by system UI |
| 3263 | status_t volStatus = checkAndSetVolume( |
| 3264 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3265 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3266 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3267 | if (volStatus != NO_ERROR) { |
| 3268 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3269 | } |
| 3270 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3271 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3272 | return status; |
| 3273 | } |
| 3274 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3275 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3276 | audio_devices_t device, |
| 3277 | IVolumeCurves &volumeCurves) |
| 3278 | { |
| 3279 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3280 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3281 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3282 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3283 | (index > volumeCurves.getVolumeIndexMax())) { |
| 3284 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 3285 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 3286 | return BAD_VALUE; |
| 3287 | } |
| 3288 | if (!audio_is_output_device(device)) { |
| 3289 | return BAD_VALUE; |
| 3290 | } |
| 3291 | |
| 3292 | // Force max volume if stream cannot be muted |
| 3293 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3294 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3295 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3296 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3297 | return NO_ERROR; |
| 3298 | } |
| 3299 | |
| 3300 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3301 | int &index, |
| 3302 | audio_devices_t device) |
| 3303 | { |
| 3304 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3305 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3306 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3307 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3308 | deviceTypes = mEngine->getOutputDevicesForAttributes( |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3309 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3310 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3311 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3315 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3316 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3317 | { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3318 | if (!isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3319 | return BAD_VALUE; |
| 3320 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3321 | index = curves.getVolumeIndex(deviceTypes); |
| 3322 | 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] | 3323 | return NO_ERROR; |
| 3324 | } |
| 3325 | |
| 3326 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3327 | int &index) |
| 3328 | { |
| 3329 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3330 | return NO_ERROR; |
| 3331 | } |
| 3332 | |
| 3333 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3334 | int &index) |
| 3335 | { |
| 3336 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3337 | return NO_ERROR; |
| 3338 | } |
| 3339 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3340 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3341 | { |
| 3342 | // select one output among several suitable for global effects. |
| 3343 | // The priority is as follows: |
| 3344 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3345 | // AudioFlinger will invalidate the track and the offloaded output |
| 3346 | // will be closed causing the effect to be moved to a PCM output. |
| 3347 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3348 | // 3: The primary output |
| 3349 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3350 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3351 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3352 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3353 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3354 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3355 | if (outputs.size() == 0) { |
| 3356 | return AUDIO_IO_HANDLE_NONE; |
| 3357 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3358 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3359 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3360 | bool activeOnly = true; |
| 3361 | |
| 3362 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3363 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 3364 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3365 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3366 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3367 | for (audio_io_handle_t output : outputs) { |
| 3368 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3369 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3370 | continue; |
| 3371 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3372 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 3373 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3374 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3375 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3376 | } |
| 3377 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3378 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3379 | } |
| 3380 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3381 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3382 | } |
| 3383 | } |
| 3384 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3385 | output = outputOffloaded; |
| 3386 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3387 | output = outputDeepBuffer; |
| 3388 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3389 | output = outputPrimary; |
| 3390 | } else { |
| 3391 | output = outputs[0]; |
| 3392 | } |
| 3393 | activeOnly = false; |
| 3394 | } |
| 3395 | |
| 3396 | if (output != mMusicEffectOutput) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3397 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3398 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 3399 | mMusicEffectOutput = output; |
| 3400 | } |
| 3401 | |
| 3402 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3403 | return output; |
| 3404 | } |
| 3405 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3406 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3407 | { |
| 3408 | return selectOutputForMusicEffects(); |
| 3409 | } |
| 3410 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3411 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3412 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3413 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3414 | int session, |
| 3415 | int id) |
| 3416 | { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3417 | if (session != AUDIO_SESSION_DEVICE) { |
| 3418 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3419 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3420 | index = mInputs.indexOfKey(io); |
| 3421 | if (index < 0) { |
| 3422 | ALOGW("registerEffect() unknown io %d", io); |
| 3423 | return INVALID_OPERATION; |
| 3424 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3425 | } |
| 3426 | } |
Eric Laurent | bf8f69f | 2022-03-25 17:48:38 +0100 | [diff] [blame] | 3427 | bool isMusicEffect = (session != AUDIO_SESSION_OUTPUT_STAGE) |
| 3428 | && ((strategy == streamToStrategy(AUDIO_STREAM_MUSIC) |
| 3429 | || strategy == PRODUCT_STRATEGY_NONE)); |
| 3430 | return mEffects.registerEffect(desc, io, session, id, isMusicEffect); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3431 | } |
| 3432 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3433 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3434 | { |
| 3435 | if (mEffects.getEffect(id) == nullptr) { |
| 3436 | return INVALID_OPERATION; |
| 3437 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3438 | if (mEffects.isEffectEnabled(id)) { |
| 3439 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3440 | setEffectEnabled(id, false); |
| 3441 | } |
| 3442 | return mEffects.unregisterEffect(id); |
| 3443 | } |
| 3444 | |
| 3445 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3446 | { |
| 3447 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3448 | if (effect == nullptr) { |
| 3449 | return INVALID_OPERATION; |
| 3450 | } |
| 3451 | |
| 3452 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3453 | if (status == NO_ERROR) { |
| 3454 | mInputs.trackEffectEnabled(effect, enabled); |
| 3455 | } |
| 3456 | return status; |
| 3457 | } |
| 3458 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3459 | |
| 3460 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3461 | { |
| 3462 | mEffects.moveEffects(ids, io); |
| 3463 | return NO_ERROR; |
| 3464 | } |
| 3465 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3466 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3467 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3468 | auto vs = toVolumeSource(stream, false); |
| 3469 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3473 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3474 | auto vs = toVolumeSource(stream, false); |
| 3475 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3476 | } |
| 3477 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3478 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3479 | { |
| 3480 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3481 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3482 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3483 | return true; |
| 3484 | } |
| 3485 | } |
| 3486 | return false; |
| 3487 | } |
| 3488 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3489 | // Register a list of custom mixes with their attributes and format. |
| 3490 | // When a mix is registered, corresponding input and output profiles are |
| 3491 | // added to the remote submix hw module. The profile contains only the |
| 3492 | // parameters (sampling rate, format...) specified by the mix. |
| 3493 | // The corresponding input remote submix device is also connected. |
| 3494 | // |
| 3495 | // When a remote submix device is connected, the address is checked to select the |
| 3496 | // appropriate profile and the corresponding input or output stream is opened. |
| 3497 | // |
| 3498 | // When capture starts, getInputForAttr() will: |
| 3499 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3500 | // - 2 if none found, getDeviceForInputSource() will: |
| 3501 | // - 2.1 look for a mix matching the attributes source |
| 3502 | // - 2.2 if none found, default to device selection by policy rules |
| 3503 | // At this time, the corresponding output remote submix device is also connected |
| 3504 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3505 | // after AudioTracks are invalidated |
| 3506 | // |
| 3507 | // When playback starts, getOutputForAttr() will: |
| 3508 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3509 | // - 2 if none found, look for a mix matching the attributes usage |
| 3510 | // - 3 if none found, default to device and output selection by policy rules. |
| 3511 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3512 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3513 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3514 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3515 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3516 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3517 | sp<HwModule> rSubmixModule; |
| 3518 | // examine each mix's route type |
| 3519 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3520 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3521 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3522 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3523 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3524 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3525 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3526 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3527 | break; |
| 3528 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3529 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3530 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3531 | 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] | 3532 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3533 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3534 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3535 | rSubmixModule = mHwModules.getModuleFromName( |
| 3536 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3537 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3538 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3539 | i); |
| 3540 | res = INVALID_OPERATION; |
| 3541 | break; |
| 3542 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3543 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3544 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3545 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3546 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3547 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3548 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3549 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3550 | } else { |
| 3551 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3552 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3553 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3554 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3555 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3556 | ALOGE("Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3557 | res = INVALID_OPERATION; |
| 3558 | break; |
| 3559 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3560 | audio_config_t outputConfig = mix.mFormat; |
| 3561 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3562 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3563 | // 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] | 3564 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3565 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3566 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3567 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3568 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3569 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3570 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3571 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3572 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3573 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
| 3574 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
| 3575 | mix.mDeviceType, address.string()); |
| 3576 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3577 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3578 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3579 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3580 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3581 | 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] | 3582 | i, mixes.size(), type, address.string()); |
| 3583 | |
| 3584 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3585 | mix.mDeviceType, mix.mDeviceAddress, |
| 3586 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3587 | if (device == nullptr) { |
| 3588 | res = INVALID_OPERATION; |
| 3589 | break; |
| 3590 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3591 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3592 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3593 | // First try to find an already opened output supporting the device |
| 3594 | 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] | 3595 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3596 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3597 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3598 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3599 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3600 | address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3601 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3602 | } else { |
| 3603 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3604 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3605 | } |
| 3606 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3607 | // If no output found, try to find a direct output profile supporting the device |
| 3608 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3609 | sp<HwModule> module = mHwModules[i]; |
| 3610 | for (size_t j = 0; |
| 3611 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3612 | j++) { |
| 3613 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3614 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3615 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3616 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3617 | address.string()); |
| 3618 | res = INVALID_OPERATION; |
| 3619 | } else { |
| 3620 | foundOutput = true; |
| 3621 | } |
| 3622 | } |
| 3623 | } |
| 3624 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3625 | if (res != NO_ERROR) { |
| 3626 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3627 | i, type, address.string()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3628 | res = INVALID_OPERATION; |
| 3629 | break; |
| 3630 | } else if (!foundOutput) { |
| 3631 | 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] | 3632 | i, type, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3633 | res = INVALID_OPERATION; |
| 3634 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3635 | } else { |
| 3636 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3637 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3638 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3639 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3640 | if (res != NO_ERROR) { |
| 3641 | unregisterPolicyMixes(mixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3642 | } else if (checkOutputs) { |
| 3643 | checkForDeviceAndOutputChanges(); |
| 3644 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3645 | } |
| 3646 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3647 | } |
| 3648 | |
| 3649 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3650 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3651 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3652 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3653 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3654 | sp<HwModule> rSubmixModule; |
| 3655 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3656 | for (const auto& mix : mixes) { |
| 3657 | 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] | 3658 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3659 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3660 | rSubmixModule = mHwModules.getModuleFromName( |
| 3661 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3662 | if (rSubmixModule == 0) { |
| 3663 | res = INVALID_OPERATION; |
| 3664 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3665 | } |
| 3666 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3667 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3668 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3669 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3670 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3671 | res = INVALID_OPERATION; |
| 3672 | continue; |
| 3673 | } |
| 3674 | |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3675 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
| 3676 | if (getDeviceConnectionState(device, address.string()) == |
| 3677 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3678 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3679 | address.string(), "remote-submix", |
| 3680 | AUDIO_FORMAT_DEFAULT); |
| 3681 | if (res != OK) { |
| 3682 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
| 3683 | "with type %d, address %s", device, address.string()); |
| 3684 | } |
| 3685 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3686 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3687 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3688 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3689 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3690 | } 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] | 3691 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3692 | res = INVALID_OPERATION; |
| 3693 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3694 | } else { |
| 3695 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3696 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3697 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3698 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3699 | if (res == NO_ERROR && checkOutputs) { |
| 3700 | checkForDeviceAndOutputChanges(); |
| 3701 | updateCallAndOutputRouting(); |
| 3702 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3703 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3704 | } |
| 3705 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3706 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3707 | { |
| 3708 | size_t i = 0; |
| 3709 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3710 | for (const auto& fmt : mManualSurroundFormats) { |
| 3711 | if (i++ != 0) dst->append(", "); |
| 3712 | std::string sfmt; |
| 3713 | FormatConverter::toString(fmt, sfmt); |
| 3714 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3715 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3716 | } |
| 3717 | } |
| 3718 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3719 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3720 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3721 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3722 | std::function<bool(audio_devices_t)> predicate, |
| 3723 | const char *context) { |
| 3724 | for (size_t i = 0; i < devices.size(); i++) { |
| 3725 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3726 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 0e26e3f | 2020-04-29 14:24:16 -0700 | [diff] [blame] | 3727 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, true /*matchAddress*/); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3728 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3729 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3730 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3731 | return false; |
| 3732 | } |
| 3733 | } |
| 3734 | return true; |
| 3735 | } |
| 3736 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3737 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3738 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3739 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3740 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3741 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3742 | } |
| 3743 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3744 | if (res != NO_ERROR) { |
| 3745 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3746 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3747 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3748 | |
| 3749 | checkForDeviceAndOutputChanges(); |
| 3750 | updateCallAndOutputRouting(); |
| 3751 | |
| 3752 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 3756 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3757 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 3758 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3759 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3760 | __FUNCTION__, uid); |
| 3761 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3762 | } |
| 3763 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3764 | checkForDeviceAndOutputChanges(); |
| 3765 | updateCallAndOutputRouting(); |
| 3766 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3767 | return res; |
| 3768 | } |
| 3769 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3770 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3771 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 3772 | device_role_t role, |
| 3773 | const AudioDeviceTypeAddrVector &devices) { |
| 3774 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3775 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3776 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3777 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3778 | return BAD_VALUE; |
| 3779 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3780 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3781 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3782 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 3783 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3784 | return status; |
| 3785 | } |
| 3786 | |
| 3787 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3788 | |
| 3789 | bool forceVolumeReeval = false; |
| 3790 | // FIXME: workaround for truncated touch sounds |
| 3791 | // to be removed when the problem is handled by system UI |
| 3792 | uint32_t delayMs = 0; |
| 3793 | if (strategy == mCommunnicationStrategy) { |
| 3794 | forceVolumeReeval = true; |
| 3795 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3796 | updateInputRouting(); |
| 3797 | } |
| 3798 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3799 | |
| 3800 | return NO_ERROR; |
| 3801 | } |
| 3802 | |
| 3803 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs) |
| 3804 | { |
| 3805 | uint32_t waitMs = 0; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3806 | bool wasLeUnicastActive = isLeUnicastActive(); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 3807 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3808 | // Only apply special touch sound delay once |
| 3809 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3810 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3811 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3812 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3813 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3814 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3815 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 3816 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3817 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 3818 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 3819 | // Let's give back to engine full device choice decision however. |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3820 | bool forceRouting = !newDevices.isEmpty(); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3821 | if (outputDesc->mUsePreferredMixerAttributes && newDevices != outputDesc->devices()) { |
| 3822 | // If the device is using preferred mixer attributes, the output need to reopen |
| 3823 | // with default configuration when the new selected devices are different from |
| 3824 | // current routing devices. |
| 3825 | outputsToReopen.emplace(mOutputs.keyAt(i), newDevices); |
| 3826 | continue; |
| 3827 | } |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3828 | waitMs = setOutputDevices(outputDesc, newDevices, forceRouting, delayMs, nullptr, |
| 3829 | true /*requiresMuteCheck*/, |
| 3830 | !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3831 | // Only apply special touch sound delay once |
| 3832 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3833 | } |
| 3834 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 3835 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 3836 | } |
| 3837 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 3838 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3839 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3840 | } |
| 3841 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3842 | void AudioPolicyManager::updateInputRouting() { |
| 3843 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3844 | // Skip for hotword recording as the input device switch |
| 3845 | // is handled within sound trigger HAL |
| 3846 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 3847 | continue; |
| 3848 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3849 | auto newDevice = getNewInputDevice(activeDesc); |
| 3850 | // Force new input selection if the new device can not be reached via current input |
| 3851 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 3852 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 3853 | } else { |
| 3854 | closeInput(activeDesc->mIoHandle); |
| 3855 | } |
| 3856 | } |
| 3857 | } |
| 3858 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3859 | status_t AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 3860 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3861 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3862 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3863 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3864 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3865 | if (status != NO_ERROR) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3866 | ALOGV("Engine could not remove preferred device for strategy %d status %d", |
| 3867 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3868 | return status; |
| 3869 | } |
| 3870 | |
| 3871 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3872 | |
| 3873 | bool forceVolumeReeval = false; |
| 3874 | // FIXME: workaround for truncated touch sounds |
| 3875 | // to be removed when the problem is handled by system UI |
| 3876 | uint32_t delayMs = 0; |
| 3877 | if (strategy == mCommunnicationStrategy) { |
| 3878 | forceVolumeReeval = true; |
| 3879 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3880 | updateInputRouting(); |
| 3881 | } |
| 3882 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3883 | |
| 3884 | return NO_ERROR; |
| 3885 | } |
| 3886 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3887 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 3888 | device_role_t role, |
| 3889 | AudioDeviceTypeAddrVector &devices) { |
| 3890 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3891 | } |
| 3892 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3893 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 3894 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3895 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3896 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3897 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3898 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3899 | return BAD_VALUE; |
| 3900 | } |
| 3901 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3902 | ALOGW_IF(status != NO_ERROR, |
| 3903 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 3904 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3905 | |
| 3906 | return status; |
| 3907 | } |
| 3908 | |
| 3909 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 3910 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3911 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3912 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3913 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3914 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3915 | return BAD_VALUE; |
| 3916 | } |
| 3917 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3918 | ALOGW_IF(status != NO_ERROR, |
| 3919 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 3920 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3921 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3922 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3923 | return status; |
| 3924 | } |
| 3925 | |
| 3926 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 3927 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 3928 | { |
| 3929 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 3930 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3931 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3932 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3933 | return BAD_VALUE; |
| 3934 | } |
| 3935 | |
| 3936 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 3937 | audioSource, role, devices); |
| 3938 | ALOGW_IF(status != NO_ERROR, |
| 3939 | "Engine could not remove devices role (%d) for capture preset %d", role, audioSource); |
| 3940 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3941 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3942 | return status; |
| 3943 | } |
| 3944 | |
| 3945 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 3946 | device_role_t role) { |
| 3947 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 3948 | |
| 3949 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
| 3950 | ALOGW_IF(status != NO_ERROR, |
| 3951 | "Engine could not clear devices role (%d) for capture preset %d", role, audioSource); |
| 3952 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3953 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3954 | return status; |
| 3955 | } |
| 3956 | |
| 3957 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 3958 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 3959 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 3960 | } |
| 3961 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3962 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3963 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3964 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3965 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3966 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3967 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3968 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 3969 | if (status != NO_ERROR) { |
| 3970 | ALOGE("%s() could not set device affinity for userId %d", |
| 3971 | __FUNCTION__, userId); |
| 3972 | return status; |
| 3973 | } |
| 3974 | |
| 3975 | // reevaluate outputs for all devices |
| 3976 | checkForDeviceAndOutputChanges(); |
| 3977 | updateCallAndOutputRouting(); |
| 3978 | |
| 3979 | return NO_ERROR; |
| 3980 | } |
| 3981 | |
| 3982 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3983 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3984 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 3985 | if (status != NO_ERROR) { |
| 3986 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 3987 | __FUNCTION__, userId); |
| 3988 | return status; |
| 3989 | } |
| 3990 | |
| 3991 | // reevaluate outputs for all devices |
| 3992 | checkForDeviceAndOutputChanges(); |
| 3993 | updateCallAndOutputRouting(); |
| 3994 | |
| 3995 | return NO_ERROR; |
| 3996 | } |
| 3997 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3998 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3999 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4000 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 4001 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4002 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 4003 | std::string stateLiteral; |
| 4004 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4005 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4006 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 4007 | "communications", "media", "record", "dock", "system", |
| 4008 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 4009 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 4010 | 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] | 4011 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 4012 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 4013 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 4014 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 4015 | dst->append(" (MANUAL: "); |
| 4016 | dumpManualSurroundFormats(dst); |
| 4017 | dst->append(")"); |
| 4018 | } |
| 4019 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4020 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4021 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 4022 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 4023 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4024 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 4025 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4026 | dst->append("\n"); |
| 4027 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 4028 | dst->append("\n"); |
| 4029 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4030 | mHwModulesAll.dump(dst); |
| 4031 | mOutputs.dump(dst); |
| 4032 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 4033 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4034 | mAudioPatches.dump(dst); |
| 4035 | mPolicyMixes.dump(dst); |
| 4036 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4037 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4038 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 4039 | for (auto& policy : mAllowedCapturePolicies) { |
| 4040 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 4041 | } |
| 4042 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4043 | dst->appendFormat(" Preferred mixer audio configuration:\n"); |
| 4044 | for (const auto it : mPreferredMixerAttrInfos) { |
| 4045 | dst->appendFormat(" - device port id: %d\n", it.first); |
| 4046 | for (const auto preferredMixerInfoIt : it.second) { |
| 4047 | dst->appendFormat(" - strategy: %d; ", preferredMixerInfoIt.first); |
| 4048 | preferredMixerInfoIt.second->dump(dst); |
| 4049 | } |
| 4050 | } |
| 4051 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4052 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 4053 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | status_t AudioPolicyManager::dump(int fd) |
| 4057 | { |
| 4058 | String8 result; |
| 4059 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 4060 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4061 | return NO_ERROR; |
| 4062 | } |
| 4063 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 4064 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 4065 | { |
| 4066 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 4067 | return NO_ERROR; |
| 4068 | } |
| 4069 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4070 | // This function checks for the parameters which can be offloaded. |
| 4071 | // This can be enhanced depending on the capability of the DSP and policy |
| 4072 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4073 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4074 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4075 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4076 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4077 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4078 | offloadInfo.format, |
| 4079 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 4080 | offloadInfo.has_video); |
| 4081 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4082 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4083 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | // See if there is a profile to support this. |
| 4087 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4088 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4089 | offloadInfo.sample_rate, |
| 4090 | offloadInfo.format, |
| 4091 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4092 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 4093 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 4094 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 4095 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 4096 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 4097 | if (profile == nullptr) { |
| 4098 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 4099 | } |
| 4100 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 4101 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 4102 | } |
| 4103 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4106 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 4107 | const audio_attributes_t& attributes) { |
| 4108 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 4109 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 4110 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 4111 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4112 | config.sample_rate, |
| 4113 | config.format, |
| 4114 | config.channel_mask, |
| 4115 | output_flags, |
| 4116 | true /* directOnly */); |
| 4117 | ALOGV("%s() profile %sfound with name: %s, " |
| 4118 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4119 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4120 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4121 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 4122 | |
| 4123 | // also try the MSD module if compatible profile not found |
| 4124 | if (profile == nullptr) { |
| 4125 | profile = getMsdProfileForOutput(outputDevices, |
| 4126 | config.sample_rate, |
| 4127 | config.format, |
| 4128 | config.channel_mask, |
| 4129 | output_flags, |
| 4130 | true /* directOnly */); |
| 4131 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 4132 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 4133 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 4134 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 4135 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 4136 | } |
| 4137 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 4138 | } |
| 4139 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4140 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 4141 | bool durationIgnored) { |
| 4142 | if (mMasterMono) { |
| 4143 | return false; // no offloading if mono is set. |
| 4144 | } |
| 4145 | |
| 4146 | // Check if offload has been disabled |
| 4147 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 4148 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 4149 | return false; |
| 4150 | } |
| 4151 | |
| 4152 | // Check if stream type is music, then only allow offload as of now. |
| 4153 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 4154 | { |
| 4155 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 4156 | return false; |
| 4157 | } |
| 4158 | |
| 4159 | //TODO: enable audio offloading with video when ready |
| 4160 | const bool allowOffloadWithVideo = |
| 4161 | property_get_bool("audio.offload.video", false /* default_value */); |
| 4162 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 4163 | ALOGV("%s: has_video == true, returning false", __func__); |
| 4164 | return false; |
| 4165 | } |
| 4166 | |
| 4167 | //If duration is less than minimum value defined in property, return false |
| 4168 | const int min_duration_secs = property_get_int32( |
| 4169 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 4170 | if (!durationIgnored) { |
| 4171 | if (min_duration_secs >= 0) { |
| 4172 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 4173 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 4174 | __func__, min_duration_secs); |
| 4175 | return false; |
| 4176 | } |
| 4177 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 4178 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 4179 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 4180 | return false; |
| 4181 | } |
| 4182 | } |
| 4183 | |
| 4184 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 4185 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 4186 | // detects there is an active non offloadable effect. |
| 4187 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 4188 | // This may prevent offloading in rare situations where effects are left active by apps |
| 4189 | // in the background. |
| 4190 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4191 | return false; |
| 4192 | } |
| 4193 | |
| 4194 | return true; |
| 4195 | } |
| 4196 | |
| 4197 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 4198 | const audio_config_t *config) { |
| 4199 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 4200 | offloadInfo.format = config->format; |
| 4201 | offloadInfo.sample_rate = config->sample_rate; |
| 4202 | offloadInfo.channel_mask = config->channel_mask; |
| 4203 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 4204 | offloadInfo.has_video = false; |
| 4205 | offloadInfo.is_streaming = false; |
| 4206 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 4207 | |
| 4208 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 4209 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4210 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4211 | // only retain flags that will drive compressed offload or passthrough |
| 4212 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 4213 | if (offloadPossible) { |
| 4214 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 4215 | } |
| 4216 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 4217 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4218 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4219 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4220 | DeviceVector outputDevices = engineOutputDevices; |
| 4221 | // the MSD module checks for different conditions and output devices |
| 4222 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4223 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 4224 | continue; |
| 4225 | } |
| 4226 | outputDevices = getMsdAudioOutDevices(); |
| 4227 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4228 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | c8f7dfc | 2022-01-06 18:42:08 +0000 | [diff] [blame] | 4229 | if (!curProfile->isCompatibleProfile(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4230 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 4231 | config->format, nullptr /*updatedFormat*/, |
| 4232 | config->channel_mask, nullptr /*updatedChannelMask*/, |
| 4233 | flags)) { |
| 4234 | continue; |
| 4235 | } |
| 4236 | // reject profiles not corresponding to a device currently available |
| 4237 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 4238 | continue; |
| 4239 | } |
| 4240 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 4241 | != AUDIO_OUTPUT_FLAG_NONE) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 4242 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4243 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 4244 | // Already reports offload gapless supported. No need to report offload support. |
| 4245 | continue; |
| 4246 | } |
| 4247 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 4248 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 4249 | // If offload gapless is reported, no need to report offload support. |
| 4250 | directMode = (audio_direct_mode_t) ((directMode & |
| 4251 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 4252 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4253 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4254 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4255 | } |
| 4256 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4257 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4258 | } |
| 4259 | } |
| 4260 | } |
| 4261 | return directMode; |
| 4262 | } |
| 4263 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4264 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4265 | AudioProfileVector& audioProfilesVector) { |
Dorin Drimus | 1711263 | 2022-09-23 15:28:51 +0000 | [diff] [blame] | 4266 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4267 | return OK; |
| 4268 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4269 | DeviceVector devices; |
| 4270 | status_t status = getDevicesForAttributes(*attr, devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4271 | if (status != OK) { |
| 4272 | return status; |
| 4273 | } |
| 4274 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4275 | if (devices.empty()) { |
| 4276 | return OK; // no output devices for the attributes |
| 4277 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4278 | return getProfilesForDevices(devices, audioProfilesVector, |
| 4279 | AUDIO_OUTPUT_FLAG_DIRECT /*flags*/, false /*isInput*/); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4280 | } |
| 4281 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4282 | status_t AudioPolicyManager::getSupportedMixerAttributes( |
| 4283 | audio_port_handle_t portId, std::vector<audio_mixer_attributes_t> &mixerAttrs) { |
| 4284 | ALOGV("%s, portId=%d", __func__, portId); |
| 4285 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4286 | if (deviceDescriptor == nullptr) { |
| 4287 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4288 | return BAD_VALUE; |
| 4289 | } |
| 4290 | for (const auto& hwModule : mHwModules) { |
| 4291 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4292 | if (curProfile->supportsDevice(deviceDescriptor)) { |
| 4293 | curProfile->toSupportedMixerAttributes(&mixerAttrs); |
| 4294 | } |
| 4295 | } |
| 4296 | } |
| 4297 | return NO_ERROR; |
| 4298 | } |
| 4299 | |
| 4300 | status_t AudioPolicyManager::setPreferredMixerAttributes( |
| 4301 | const audio_attributes_t *attr, |
| 4302 | audio_port_handle_t portId, |
| 4303 | uid_t uid, |
| 4304 | const audio_mixer_attributes_t *mixerAttributes) { |
| 4305 | ALOGV("%s, attr=%s, mixerAttributes={format=%#x, channelMask=%#x, samplingRate=%u, " |
| 4306 | "mixerBehavior=%d}, uid=%d, portId=%u", |
| 4307 | __func__, toString(*attr).c_str(), mixerAttributes->config.format, |
| 4308 | mixerAttributes->config.channel_mask, mixerAttributes->config.sample_rate, |
| 4309 | mixerAttributes->mixer_behavior, uid, portId); |
| 4310 | if (attr->usage != AUDIO_USAGE_MEDIA) { |
| 4311 | ALOGE("%s failed, only media is allowed, the given usage is %d", __func__, attr->usage); |
| 4312 | return BAD_VALUE; |
| 4313 | } |
| 4314 | sp<DeviceDescriptor> deviceDescriptor = mAvailableOutputDevices.getDeviceFromId(portId); |
| 4315 | if (deviceDescriptor == nullptr) { |
| 4316 | ALOGE("%s the requested device is currently unavailable", __func__); |
| 4317 | return BAD_VALUE; |
| 4318 | } |
| 4319 | if (!audio_is_usb_out_device(deviceDescriptor->type())) { |
| 4320 | ALOGE("%s(%d), type=%d, is not a usb output device", |
| 4321 | __func__, portId, deviceDescriptor->type()); |
| 4322 | return BAD_VALUE; |
| 4323 | } |
| 4324 | |
| 4325 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4326 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4327 | flags = (audio_output_flags_t) (flags | |
| 4328 | audio_output_flags_from_mixer_behavior(mixerAttributes->mixer_behavior)); |
| 4329 | sp<IOProfile> profile = nullptr; |
| 4330 | DeviceVector devices(deviceDescriptor); |
| 4331 | for (const auto& hwModule : mHwModules) { |
| 4332 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
| 4333 | if (curProfile->hasDynamicAudioProfile() |
| 4334 | && curProfile->isCompatibleProfile(devices, |
| 4335 | mixerAttributes->config.sample_rate, |
| 4336 | nullptr /*updatedSamplingRate*/, |
| 4337 | mixerAttributes->config.format, |
| 4338 | nullptr /*updatedFormat*/, |
| 4339 | mixerAttributes->config.channel_mask, |
| 4340 | nullptr /*updatedChannelMask*/, |
| 4341 | flags, |
| 4342 | false /*exactMatchRequiredForInputFlags*/)) { |
| 4343 | profile = curProfile; |
| 4344 | break; |
| 4345 | } |
| 4346 | } |
| 4347 | } |
| 4348 | if (profile == nullptr) { |
| 4349 | ALOGE("%s, there is no compatible profile found", __func__); |
| 4350 | return BAD_VALUE; |
| 4351 | } |
| 4352 | |
| 4353 | sp<PreferredMixerAttributesInfo> mixerAttrInfo = |
| 4354 | sp<PreferredMixerAttributesInfo>::make( |
| 4355 | uid, portId, profile, flags, *mixerAttributes); |
| 4356 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4357 | mPreferredMixerAttrInfos[portId][strategy] = mixerAttrInfo; |
| 4358 | |
| 4359 | // If 1) there is any client from the preferred mixer configuration owner that is currently |
| 4360 | // active and matches the strategy and 2) current output is on the preferred device and the |
| 4361 | // mixer configuration doesn't match the preferred one, reopen output with preferred mixer |
| 4362 | // configuration. |
| 4363 | std::vector<audio_io_handle_t> outputsToReopen; |
| 4364 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4365 | const auto output = mOutputs.valueAt(i); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4366 | if (output->mProfile == profile && output->devices().onlyContainsDevice(deviceDescriptor)) { |
| 4367 | if (output->isConfigurationMatched(mixerAttributes->config, flags)) { |
| 4368 | output->mUsePreferredMixerAttributes = true; |
| 4369 | } else { |
| 4370 | for (const auto &client: output->getActiveClients()) { |
| 4371 | if (client->uid() == uid && client->strategy() == strategy) { |
| 4372 | client->setIsInvalid(); |
| 4373 | outputsToReopen.push_back(output->mIoHandle); |
| 4374 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4375 | } |
| 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4380 | config.sample_rate = mixerAttributes->config.sample_rate; |
| 4381 | config.channel_mask = mixerAttributes->config.channel_mask; |
| 4382 | config.format = mixerAttributes->config.format; |
| 4383 | for (const auto output : outputsToReopen) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 4384 | sp<SwAudioOutputDescriptor> desc = |
| 4385 | reopenOutput(mOutputs.valueFor(output), &config, flags, __func__); |
| 4386 | if (desc == nullptr) { |
| 4387 | ALOGE("%s, failed to reopen output with preferred mixer attributes", __func__); |
| 4388 | continue; |
| 4389 | } |
| 4390 | desc->mUsePreferredMixerAttributes = true; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
| 4393 | return NO_ERROR; |
| 4394 | } |
| 4395 | |
| 4396 | sp<PreferredMixerAttributesInfo> AudioPolicyManager::getPreferredMixerAttributesInfo( |
| 4397 | audio_port_handle_t devicePortId, product_strategy_t strategy) { |
| 4398 | auto it = mPreferredMixerAttrInfos.find(devicePortId); |
| 4399 | if (it == mPreferredMixerAttrInfos.end()) { |
| 4400 | return nullptr; |
| 4401 | } |
| 4402 | auto mixerAttrInfoIt = it->second.find(strategy); |
| 4403 | if (mixerAttrInfoIt == it->second.end()) { |
| 4404 | return nullptr; |
| 4405 | } |
| 4406 | return mixerAttrInfoIt->second; |
| 4407 | } |
| 4408 | |
| 4409 | status_t AudioPolicyManager::getPreferredMixerAttributes( |
| 4410 | const audio_attributes_t *attr, |
| 4411 | audio_port_handle_t portId, |
| 4412 | audio_mixer_attributes_t* mixerAttributes) { |
| 4413 | sp<PreferredMixerAttributesInfo> info = getPreferredMixerAttributesInfo( |
| 4414 | portId, mEngine->getProductStrategyForAttributes(*attr)); |
| 4415 | if (info == nullptr) { |
| 4416 | return NAME_NOT_FOUND; |
| 4417 | } |
| 4418 | *mixerAttributes = info->getMixerAttributes(); |
| 4419 | return NO_ERROR; |
| 4420 | } |
| 4421 | |
| 4422 | status_t AudioPolicyManager::clearPreferredMixerAttributes(const audio_attributes_t *attr, |
| 4423 | audio_port_handle_t portId, |
| 4424 | uid_t uid) { |
| 4425 | const product_strategy_t strategy = mEngine->getProductStrategyForAttributes(*attr); |
| 4426 | const auto preferredMixerAttrInfo = getPreferredMixerAttributesInfo(portId, strategy); |
| 4427 | if (preferredMixerAttrInfo == nullptr) { |
| 4428 | return NAME_NOT_FOUND; |
| 4429 | } |
| 4430 | if (preferredMixerAttrInfo->getUid() != uid) { |
| 4431 | ALOGE("%s, requested uid=%d, owned uid=%d", |
| 4432 | __func__, uid, preferredMixerAttrInfo->getUid()); |
| 4433 | return PERMISSION_DENIED; |
| 4434 | } |
| 4435 | mPreferredMixerAttrInfos[portId].erase(strategy); |
| 4436 | if (mPreferredMixerAttrInfos[portId].empty()) { |
| 4437 | mPreferredMixerAttrInfos.erase(portId); |
| 4438 | } |
| 4439 | |
| 4440 | // Reconfig existing output |
| 4441 | std::vector<audio_io_handle_t> potentialOutputsToReopen; |
| 4442 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4443 | if (mOutputs.valueAt(i)->mProfile == preferredMixerAttrInfo->getProfile()) { |
| 4444 | potentialOutputsToReopen.push_back(mOutputs.keyAt(i)); |
| 4445 | } |
| 4446 | } |
| 4447 | for (const auto output : potentialOutputsToReopen) { |
| 4448 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 4449 | if (desc->isConfigurationMatched(preferredMixerAttrInfo->getConfigBase(), |
| 4450 | preferredMixerAttrInfo->getFlags())) { |
| 4451 | reopenOutput(desc, nullptr /*config*/, AUDIO_OUTPUT_FLAG_NONE, __func__); |
| 4452 | } |
| 4453 | } |
| 4454 | return NO_ERROR; |
| 4455 | } |
| 4456 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4457 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 4458 | audio_port_type_t type, |
| 4459 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4460 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4461 | unsigned int *generation) |
| 4462 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4463 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 4464 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4465 | return BAD_VALUE; |
| 4466 | } |
| 4467 | 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] | 4468 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4469 | *num_ports = 0; |
| 4470 | } |
| 4471 | |
| 4472 | size_t portsWritten = 0; |
| 4473 | size_t portsMax = *num_ports; |
| 4474 | *num_ports = 0; |
| 4475 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4476 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 4477 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4478 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4479 | for (const auto& dev : mAvailableOutputDevices) { |
| 4480 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4481 | continue; |
| 4482 | } |
| 4483 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4484 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4485 | } |
| 4486 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4487 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4488 | } |
| 4489 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4490 | for (const auto& dev : mAvailableInputDevices) { |
| 4491 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4492 | continue; |
| 4493 | } |
| 4494 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4495 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4496 | } |
| 4497 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4498 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 4502 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 4503 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 4504 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4505 | } |
| 4506 | *num_ports += mInputs.size(); |
| 4507 | } |
| 4508 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4509 | size_t numOutputs = 0; |
| 4510 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4511 | if (!mOutputs[i]->isDuplicated()) { |
| 4512 | numOutputs++; |
| 4513 | if (portsWritten < portsMax) { |
| 4514 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4515 | } |
| 4516 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4517 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4518 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4519 | } |
| 4520 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 4521 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4522 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4523 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4524 | return NO_ERROR; |
| 4525 | } |
| 4526 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4527 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4528 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 4529 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 4530 | return BAD_VALUE; |
| 4531 | } |
| 4532 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 4533 | if (dev != 0) { |
| 4534 | dev->toAudioPort(port); |
| 4535 | return NO_ERROR; |
| 4536 | } |
| 4537 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 4538 | if (dev != 0) { |
| 4539 | dev->toAudioPort(port); |
| 4540 | return NO_ERROR; |
| 4541 | } |
| 4542 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 4543 | if (out != 0) { |
| 4544 | out->toAudioPort(port); |
| 4545 | return NO_ERROR; |
| 4546 | } |
| 4547 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 4548 | if (in != 0) { |
| 4549 | in->toAudioPort(port); |
| 4550 | return NO_ERROR; |
| 4551 | } |
| 4552 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4553 | } |
| 4554 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4555 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 4556 | audio_patch_handle_t *handle, |
| 4557 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4558 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4559 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4560 | if (handle == NULL || patch == NULL) { |
| 4561 | return BAD_VALUE; |
| 4562 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4563 | 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] | 4564 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4565 | return BAD_VALUE; |
| 4566 | } |
| 4567 | // only one source per audio patch supported for now |
| 4568 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4569 | return INVALID_OPERATION; |
| 4570 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4571 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4572 | return INVALID_OPERATION; |
| 4573 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4574 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4575 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 4576 | return INVALID_OPERATION; |
| 4577 | } |
| 4578 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4579 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4580 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 4581 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 4582 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 4583 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 4584 | return BAD_VALUE; |
| 4585 | } |
| 4586 | ALOGV("%s between source %s and sink %s", __func__, |
| 4587 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 4588 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 4589 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 4590 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 4591 | const struct audio_port_config *source = &patch->sources[0]; |
| 4592 | sp<SourceClientDescriptor> sourceDesc = |
| 4593 | new InternalSourceClientDescriptor( |
| 4594 | portId, uid, attributes, *source, srcDevice, sinkDevice, |
| 4595 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes)); |
| 4596 | |
| 4597 | status_t status = |
| 4598 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 4599 | |
| 4600 | if (status != NO_ERROR) { |
| 4601 | return INVALID_OPERATION; |
| 4602 | } |
| 4603 | mAudioSources.add(portId, sourceDesc); |
| 4604 | return NO_ERROR; |
| 4605 | } |
| 4606 | |
| 4607 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 4608 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 4609 | const struct audio_patch *patch, |
| 4610 | audio_patch_handle_t &handle, |
| 4611 | uid_t uid, uint32_t delayMs) |
| 4612 | { |
| 4613 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 4614 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4615 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4616 | return INVALID_OPERATION; |
| 4617 | } |
| 4618 | sourceDesc->connect(handle, sinkDevice); |
| 4619 | if (isMsdPatch(handle)) { |
| 4620 | return NO_ERROR; |
| 4621 | } |
| 4622 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4623 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4624 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 4625 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 4626 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 4627 | goto FailurePatchAdded; |
| 4628 | } |
| 4629 | status = swOutput->start(); |
| 4630 | if (status != NO_ERROR) { |
| 4631 | goto FailureSourceAdded; |
| 4632 | } |
| 4633 | swOutput->addClient(sourceDesc); |
| 4634 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 4635 | if (status != NO_ERROR) { |
| 4636 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4637 | goto FailureSourceActive; |
| 4638 | } |
| 4639 | if (delayMs != 0) { |
| 4640 | usleep(delayMs * 1000); |
| 4641 | } |
| 4642 | return NO_ERROR; |
| 4643 | |
| 4644 | FailureSourceActive: |
| 4645 | swOutput->stop(); |
| 4646 | releaseOutput(sourceDesc->portId()); |
| 4647 | FailureSourceAdded: |
| 4648 | sourceDesc->setSwOutput(nullptr); |
| 4649 | FailurePatchAdded: |
| 4650 | releaseAudioPatchInternal(handle); |
| 4651 | return INVALID_OPERATION; |
| 4652 | } |
| 4653 | |
| 4654 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 4655 | audio_patch_handle_t *handle, |
| 4656 | uid_t uid, uint32_t delayMs, |
| 4657 | const sp<SourceClientDescriptor>& sourceDesc) |
| 4658 | { |
| 4659 | 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] | 4660 | sp<AudioPatch> patchDesc; |
| 4661 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 4662 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4663 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 4664 | patch->sources[0].role, |
| 4665 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4666 | #if LOG_NDEBUG == 0 |
| 4667 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4668 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 4669 | patch->sinks[i].role, |
| 4670 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4671 | } |
| 4672 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4673 | |
| 4674 | if (index >= 0) { |
| 4675 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4676 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 4677 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4678 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4679 | return INVALID_OPERATION; |
| 4680 | } |
| 4681 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4682 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
| 4685 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4686 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4687 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4688 | 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] | 4689 | return BAD_VALUE; |
| 4690 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4691 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 4692 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4693 | if (patchDesc != 0) { |
| 4694 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4695 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 4696 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4697 | return BAD_VALUE; |
| 4698 | } |
| 4699 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4700 | DeviceVector devices; |
| 4701 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4702 | // Only support mix to devices connection |
| 4703 | // TODO add support for mix to mix connection |
| 4704 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4705 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4706 | return INVALID_OPERATION; |
| 4707 | } |
| 4708 | sp<DeviceDescriptor> devDesc = |
| 4709 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 4710 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4711 | 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] | 4712 | return BAD_VALUE; |
| 4713 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4714 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4715 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4716 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4717 | NULL, // updatedSamplingRate |
| 4718 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4719 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4720 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4721 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4722 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4723 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4724 | return INVALID_OPERATION; |
| 4725 | } |
| 4726 | devices.add(devDesc); |
| 4727 | } |
| 4728 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4729 | return INVALID_OPERATION; |
| 4730 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4731 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4732 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4733 | ALOGV("%s setting device %s on output %d", |
| 4734 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4735 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4736 | index = mAudioPatches.indexOfKey(*handle); |
| 4737 | if (index >= 0) { |
| 4738 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4739 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4740 | } |
| 4741 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4742 | patchDesc->setUid(uid); |
| 4743 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4744 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4745 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4746 | return INVALID_OPERATION; |
| 4747 | } |
| 4748 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4749 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 4750 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4751 | // only one sink supported when connecting an input device to a mix |
| 4752 | if (patch->num_sinks > 1) { |
| 4753 | return INVALID_OPERATION; |
| 4754 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4755 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4756 | if (inputDesc == NULL) { |
| 4757 | return BAD_VALUE; |
| 4758 | } |
| 4759 | if (patchDesc != 0) { |
| 4760 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 4761 | return BAD_VALUE; |
| 4762 | } |
| 4763 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4764 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4765 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4766 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4767 | return BAD_VALUE; |
| 4768 | } |
| 4769 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4770 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4771 | patch->sinks[0].sample_rate, |
| 4772 | NULL, /*updatedSampleRate*/ |
| 4773 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4774 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4775 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4776 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4777 | // FIXME for the parameter type, |
| 4778 | // and the NONE |
| 4779 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4780 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4781 | return INVALID_OPERATION; |
| 4782 | } |
| 4783 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4784 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4785 | device->toString().c_str(), inputDesc->mIoHandle); |
| 4786 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4787 | index = mAudioPatches.indexOfKey(*handle); |
| 4788 | if (index >= 0) { |
| 4789 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4790 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4791 | } |
| 4792 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4793 | patchDesc->setUid(uid); |
| 4794 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4795 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4796 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4797 | return INVALID_OPERATION; |
| 4798 | } |
| 4799 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4800 | // device to device connection |
| 4801 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4802 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4803 | return BAD_VALUE; |
| 4804 | } |
| 4805 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4806 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4807 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4808 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 4809 | return BAD_VALUE; |
| 4810 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4811 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4812 | //update source and sink with our own data as the data passed in the patch may |
| 4813 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4814 | PatchBuilder patchBuilder; |
| 4815 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4816 | |
| 4817 | // if first sink is to MSD, establish single MSD patch |
| 4818 | if (getMsdAudioOutDevices().contains( |
| 4819 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 4820 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 4821 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 4822 | goto installPatch; |
| 4823 | } |
| 4824 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4825 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 4826 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4827 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4828 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4829 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4830 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4831 | return INVALID_OPERATION; |
| 4832 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4833 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4834 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4835 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4836 | return BAD_VALUE; |
| 4837 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4838 | audio_port_config sinkPortConfig = {}; |
| 4839 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 4840 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4841 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4842 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 4843 | // volume management purpose (tracking activity) |
| 4844 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 4845 | // in config XML to reach the sink so that is can be declared as available. |
| 4846 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 4847 | sp<SwAudioOutputDescriptor> outputDesc; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4848 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4849 | // take care of dynamic routing for SwOutput selection, |
| 4850 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 4851 | audio_stream_type_t stream = sourceDesc->stream(); |
| 4852 | audio_attributes_t resultAttr; |
| 4853 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4854 | config.sample_rate = sourceDesc->config().sample_rate; |
François Gaffie | 2a24db4 | 2022-04-04 16:45:02 +0200 | [diff] [blame] | 4855 | audio_channel_mask_t sourceMask = sourceDesc->config().channel_mask; |
| 4856 | config.channel_mask = |
| 4857 | (audio_channel_mask_get_representation(sourceMask) |
| 4858 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) ? sourceMask |
| 4859 | : audio_channel_mask_in_to_out(sourceMask); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4860 | config.format = sourceDesc->config().format; |
| 4861 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4862 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 4863 | bool isRequestedDeviceForExclusiveUse = false; |
| 4864 | output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4865 | bool isSpatialized; |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4866 | bool isBitPerfect; |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4867 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 4868 | &stream, sourceDesc->uid(), &config, &flags, |
| 4869 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
jiabin | c658e45 | 2022-10-21 20:52:21 +0000 | [diff] [blame] | 4870 | nullptr, &outputType, &isSpatialized, &isBitPerfect); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4871 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4872 | ALOGV("%s no output for device %s", |
| 4873 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 4874 | return INVALID_OPERATION; |
| 4875 | } |
| 4876 | outputDesc = mOutputs.valueFor(output); |
| 4877 | if (outputDesc->isDuplicated()) { |
| 4878 | ALOGE("%s output is duplicated", __func__); |
| 4879 | return INVALID_OPERATION; |
| 4880 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 4881 | bool closeOutput = outputDesc->mDirectOpenCount != 0; |
| 4882 | sourceDesc->setSwOutput(outputDesc, closeOutput); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4883 | } else { |
| 4884 | // Same for "raw patches" aka created from createAudioPatch API |
| 4885 | SortedVector<audio_io_handle_t> outputs = |
| 4886 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 4887 | // if the sink device is reachable via an opened output stream, request to |
| 4888 | // go via this output stream by adding a second source to the patch |
| 4889 | // description |
| 4890 | output = selectOutput(outputs); |
| 4891 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4892 | ALOGE("%s no output available for internal patch sink", __func__); |
| 4893 | return INVALID_OPERATION; |
| 4894 | } |
| 4895 | outputDesc = mOutputs.valueFor(output); |
| 4896 | if (outputDesc->isDuplicated()) { |
| 4897 | ALOGV("%s output for device %s is duplicated", |
| 4898 | __func__, sinkDevice->toString().c_str()); |
| 4899 | return INVALID_OPERATION; |
| 4900 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 4901 | sourceDesc->setSwOutput(outputDesc, /* closeOutput= */ false); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4902 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4903 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 4904 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4905 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 4906 | // - 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] | 4907 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 4908 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4909 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 4910 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4911 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4912 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4913 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4914 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4915 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4916 | return INVALID_OPERATION; |
| 4917 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4918 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4919 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4920 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 4921 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4922 | // for volume control, we may need a valid stream |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 4923 | srcMixPortConfig.ext.mix.usecase.stream = |
| 4924 | (!sourceDesc->isInternal() || isCallTxAudioSource(sourceDesc)) ? |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4925 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 4926 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4927 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4928 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4929 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4930 | } |
| 4931 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 4932 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4933 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4934 | status_t status = installPatch( |
| 4935 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 4936 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4937 | 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] | 4938 | return INVALID_OPERATION; |
| 4939 | } |
| 4940 | } else { |
| 4941 | return BAD_VALUE; |
| 4942 | } |
| 4943 | } else { |
| 4944 | return BAD_VALUE; |
| 4945 | } |
| 4946 | return NO_ERROR; |
| 4947 | } |
| 4948 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4949 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4950 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4951 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4952 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 4953 | |
| 4954 | if (index < 0) { |
| 4955 | return BAD_VALUE; |
| 4956 | } |
| 4957 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4958 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 4959 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4960 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4961 | return INVALID_OPERATION; |
| 4962 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4963 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 4964 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 4965 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 4966 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 4967 | portId = sourceDesc->portId(); |
| 4968 | break; |
| 4969 | } |
| 4970 | } |
| 4971 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 4972 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4973 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4974 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4975 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4976 | uint32_t delayMs, |
| 4977 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4978 | { |
| 4979 | ALOGV("%s patch %d", __func__, handle); |
| 4980 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 4981 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 4982 | return BAD_VALUE; |
| 4983 | } |
| 4984 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4985 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4986 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4987 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4988 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4989 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4990 | 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] | 4991 | return BAD_VALUE; |
| 4992 | } |
| 4993 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4994 | setOutputDevices(outputDesc, |
| 4995 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 4996 | true, |
| 4997 | 0, |
| 4998 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4999 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 5000 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5001 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5002 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5003 | 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] | 5004 | return BAD_VALUE; |
| 5005 | } |
| 5006 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5007 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5008 | true, |
| 5009 | NULL); |
| 5010 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5011 | status_t status = |
| 5012 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 5013 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 5014 | __func__, status, patchDesc->getAfHandle()); |
| 5015 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5016 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5017 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5018 | // SW or HW Bridge |
| 5019 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 5020 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 5021 | 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] | 5022 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 5023 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 5024 | outputDesc = sourceDesc->swOutput().promote(); |
| 5025 | } |
| 5026 | if (outputDesc == nullptr) { |
| 5027 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 5028 | // releaseOutput has already called closeOutput in case of direct output |
| 5029 | return NO_ERROR; |
| 5030 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame] | 5031 | patchHandle = outputDesc->getPatchHandle(); |
| 5032 | // When a Sw bridge is released, the mixer used by this bridge will release its |
| 5033 | // patch at AudioFlinger side. Hence, the mixer audio patch must be recreated |
| 5034 | // Reuse patch handle to force audio flinger removing initial mixer patch removal |
| 5035 | // updating hal patch handle (prevent leaks). |
| 5036 | // While using a HwBridge, force reconsidering device only if not reusing an existing |
| 5037 | // output and no more activity on output (will force to close). |
| 5038 | bool force = sourceDesc->useSwBridge() || |
| 5039 | (sourceDesc->canCloseOutput() && !outputDesc->isActive()); |
| 5040 | // APM pattern is to have always outputs opened / patch realized for reachable devices. |
| 5041 | // Update device may result to NONE (empty), coupled with force, it releases the patch. |
| 5042 | // Reconsider device only for cases: |
| 5043 | // 1 / Active Output |
| 5044 | // 2 / Inactive Output previously hosting HwBridge |
| 5045 | // 3 / Inactive Output previously hosting SwBridge that can be closed. |
| 5046 | bool updateDevice = outputDesc->isActive() || !sourceDesc->useSwBridge() || |
| 5047 | sourceDesc->canCloseOutput(); |
| 5048 | setOutputDevices(outputDesc, |
| 5049 | updateDevice ? getNewOutputDevices(outputDesc, true /*fromCache*/) : |
| 5050 | outputDesc->devices(), |
| 5051 | force, |
| 5052 | 0, |
| 5053 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5054 | } else { |
| 5055 | return BAD_VALUE; |
| 5056 | } |
| 5057 | } else { |
| 5058 | return BAD_VALUE; |
| 5059 | } |
| 5060 | return NO_ERROR; |
| 5061 | } |
| 5062 | |
| 5063 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 5064 | struct audio_patch *patches, |
| 5065 | unsigned int *generation) |
| 5066 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5067 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5068 | return BAD_VALUE; |
| 5069 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5070 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5071 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5074 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5075 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5076 | ALOGV("setAudioPortConfig()"); |
| 5077 | |
| 5078 | if (config == NULL) { |
| 5079 | return BAD_VALUE; |
| 5080 | } |
| 5081 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 5082 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5083 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 5084 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5085 | } |
| 5086 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5087 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5088 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 5089 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5090 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5091 | if (outputDesc == NULL) { |
| 5092 | return BAD_VALUE; |
| 5093 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 5094 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 5095 | "setAudioPortConfig() called on duplicated output %d", |
| 5096 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5097 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5098 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5099 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5100 | if (inputDesc == NULL) { |
| 5101 | return BAD_VALUE; |
| 5102 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5103 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5104 | } else { |
| 5105 | return BAD_VALUE; |
| 5106 | } |
| 5107 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 5108 | sp<DeviceDescriptor> deviceDesc; |
| 5109 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 5110 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 5111 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 5112 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 5113 | } else { |
| 5114 | return BAD_VALUE; |
| 5115 | } |
| 5116 | if (deviceDesc == NULL) { |
| 5117 | return BAD_VALUE; |
| 5118 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5119 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5120 | } else { |
| 5121 | return BAD_VALUE; |
| 5122 | } |
| 5123 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5124 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5125 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 5126 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 5127 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5128 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 5129 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5130 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 5131 | if (status != NO_ERROR) { |
| 5132 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5133 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 5134 | |
| 5135 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5136 | } |
| 5137 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5138 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 5139 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5140 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5141 | clearAudioPatches(uid); |
| 5142 | clearSessionRoutes(uid); |
| 5143 | } |
| 5144 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5145 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 5146 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5147 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5148 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5149 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 5150 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | } |
| 5154 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5155 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5156 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5157 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 5158 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 5159 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 5160 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5161 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5162 | std::map<audio_io_handle_t, DeviceVector> outputsToReopen; |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5163 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 5164 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 5165 | continue; |
| 5166 | } |
| 5167 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5168 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5169 | continue; |
| 5170 | } |
| 5171 | // If the default device for this strategy is on another output mix, |
| 5172 | // invalidate all tracks in this strategy to force re connection. |
| 5173 | // Otherwise select new device on the output mix. |
| 5174 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5175 | for (auto stream : mEngine->getStreamTypesForProductStrategy(ps)) { |
| 5176 | mpClientInterface->invalidateStream(stream); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5177 | } |
| 5178 | } else { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5179 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 5180 | if (outputDesc->mUsePreferredMixerAttributes && outputDesc->devices() != newDevices) { |
| 5181 | // If the device is using preferred mixer attributes, the output need to reopen |
| 5182 | // with default configuration when the new selected devices are different from |
| 5183 | // current routing devices. |
| 5184 | outputsToReopen.emplace(mOutputs.keyAt(j), newDevices); |
| 5185 | continue; |
| 5186 | } |
| 5187 | setOutputDevices(outputDesc, newDevices, false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5188 | } |
| 5189 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 5190 | reopenOutputsWithDevices(outputsToReopen); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 5194 | { |
| 5195 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5196 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5197 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5198 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5199 | for (const auto& client : outputDesc->getClientIterable()) { |
| 5200 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5201 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5202 | auto clientStrategy = client->strategy(); |
| 5203 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 5204 | end(affectedStrategies)) { |
| 5205 | continue; |
| 5206 | } |
| 5207 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5208 | } |
| 5209 | } |
| 5210 | } |
| 5211 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5212 | for (const auto& strategy : affectedStrategies) { |
| 5213 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | // remove input routes associated with this uid |
| 5217 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5218 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5219 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 5220 | for (const auto& client : inputDesc->getClientIterable()) { |
| 5221 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 5222 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 5223 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5224 | } |
| 5225 | } |
| 5226 | } |
| 5227 | // reroute inputs if necessary |
| 5228 | SortedVector<audio_io_handle_t> inputsToClose; |
| 5229 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5230 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5231 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5232 | inputsToClose.add(inputDesc->mIoHandle); |
| 5233 | } |
| 5234 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5235 | for (const auto& input : inputsToClose) { |
| 5236 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5237 | } |
| 5238 | } |
| 5239 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5240 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 5241 | { |
| 5242 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5243 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5244 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5245 | stopAudioSource(mAudioSources.keyAt(i)); |
| 5246 | } |
| 5247 | } |
| 5248 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5249 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5250 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 5251 | audio_io_handle_t *ioHandle, |
| 5252 | audio_devices_t *device) |
| 5253 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 5254 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 5255 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5256 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5257 | *device = mEngine->getInputDeviceForAttributes(attr)->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5258 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 5259 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5260 | } |
| 5261 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5262 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5263 | const audio_attributes_t *attributes, |
| 5264 | audio_port_handle_t *portId, |
| 5265 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5266 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5267 | ALOGV("%s", __FUNCTION__); |
| 5268 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 5269 | |
| 5270 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 5271 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 5272 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5273 | return BAD_VALUE; |
| 5274 | } |
| 5275 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5276 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 5277 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5278 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 5279 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5280 | return INVALID_OPERATION; |
| 5281 | } |
| 5282 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5283 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5284 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5285 | String8(source->ext.device.address), |
| 5286 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5287 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5288 | 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] | 5289 | return BAD_VALUE; |
| 5290 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5291 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5292 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5293 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5294 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5295 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5296 | mEngine->getStreamTypeForAttributes(*attributes), |
| 5297 | mEngine->getProductStrategyForAttributes(*attributes), |
| 5298 | toVolumeSource(*attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5299 | |
| 5300 | status_t status = connectAudioSource(sourceDesc); |
| 5301 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5302 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5303 | } |
| 5304 | return status; |
| 5305 | } |
| 5306 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 5307 | sp<SourceClientDescriptor> AudioPolicyManager::startAudioSourceInternal( |
| 5308 | const struct audio_port_config *source, const audio_attributes_t *attributes, uid_t uid) |
| 5309 | { |
| 5310 | ALOGV("%s", __FUNCTION__); |
| 5311 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 5312 | |
| 5313 | status_t status = startAudioSource(source, attributes, &portId, uid); |
| 5314 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 5315 | return mAudioSources.valueFor(portId); |
| 5316 | } |
| 5317 | |
| 5318 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5319 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5320 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5321 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5322 | |
| 5323 | // make sure we only have one patch per source. |
| 5324 | disconnectAudioSource(sourceDesc); |
| 5325 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5326 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5327 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 5328 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 5329 | sourceDesc->srcDevice()->type(), |
| 5330 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 5331 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5332 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5333 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5334 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5335 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5336 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 5337 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 5338 | return INVALID_OPERATION; |
| 5339 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5340 | PatchBuilder patchBuilder; |
| 5341 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 5342 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5343 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5344 | return connectAudioSourceToSink( |
| 5345 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, 0 /*delayMs*/); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5348 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5349 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5350 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 5351 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5352 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5353 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5354 | return BAD_VALUE; |
| 5355 | } |
| 5356 | status_t status = disconnectAudioSource(sourceDesc); |
| 5357 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5358 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5359 | return status; |
| 5360 | } |
| 5361 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5362 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 5363 | { |
| 5364 | if (mMasterMono == mono) { |
| 5365 | return NO_ERROR; |
| 5366 | } |
| 5367 | mMasterMono = mono; |
| 5368 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 5369 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 5370 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 5371 | // |
| 5372 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 5373 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 5374 | // play back offloaded. |
| 5375 | if (mMasterMono) { |
| 5376 | Vector<audio_io_handle_t> offloaded; |
| 5377 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5378 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5379 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 5380 | offloaded.push(desc->mIoHandle); |
| 5381 | } |
| 5382 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5383 | for (const auto& handle : offloaded) { |
| 5384 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5385 | } |
| 5386 | } |
| 5387 | // update master mono for all remaining outputs |
| 5388 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5389 | updateMono(mOutputs.keyAt(i)); |
| 5390 | } |
| 5391 | return NO_ERROR; |
| 5392 | } |
| 5393 | |
| 5394 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 5395 | { |
| 5396 | *mono = mMasterMono; |
| 5397 | return NO_ERROR; |
| 5398 | } |
| 5399 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5400 | float AudioPolicyManager::getStreamVolumeDB( |
| 5401 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 5402 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5403 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5404 | } |
| 5405 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5406 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 5407 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5408 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5409 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5410 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 5411 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5412 | return BAD_VALUE; |
| 5413 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5414 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 5415 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5416 | |
| 5417 | size_t formatsWritten = 0; |
| 5418 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 5419 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5420 | *numSurroundFormats = mConfig.getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5421 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5422 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5423 | for (const auto& format: mConfig.getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5424 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5425 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5426 | bool formatEnabled = true; |
| 5427 | switch (forceUse) { |
| 5428 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5429 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5430 | break; |
| 5431 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 5432 | formatEnabled = false; |
| 5433 | break; |
| 5434 | default: // AUTO or ALWAYS => true |
| 5435 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5436 | } |
| 5437 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 5438 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5439 | } |
| 5440 | return NO_ERROR; |
| 5441 | } |
| 5442 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5443 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 5444 | audio_format_t *surroundFormats) { |
| 5445 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 5446 | return BAD_VALUE; |
| 5447 | } |
| 5448 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 5449 | __func__, *numSurroundFormats, surroundFormats); |
| 5450 | |
| 5451 | size_t formatsWritten = 0; |
| 5452 | size_t formatsMax = *numSurroundFormats; |
| 5453 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 5454 | |
| 5455 | // Return formats from all device profiles that have already been resolved by |
| 5456 | // checkOutputsForDevice(). |
| 5457 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 5458 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 5459 | audio_devices_t deviceType = device->type(); |
| 5460 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 5461 | // returns formats reported by HDMI devices. |
| 5462 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 5463 | continue; |
| 5464 | } |
| 5465 | // Formats reported by sink devices |
| 5466 | std::unordered_set<audio_format_t> formatset; |
| 5467 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 5468 | formatset.insert(it->second.begin(), it->second.end()); |
| 5469 | } |
| 5470 | |
| 5471 | // Formats hard-coded in the in policy configuration file (if any). |
| 5472 | FormatVector encodedFormats = device->encodedFormats(); |
| 5473 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 5474 | // Filter the formats which are supported by the vendor hardware. |
| 5475 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
| 5476 | if (mConfig.getSurroundFormats().count(*it) != 0) { |
| 5477 | formats.insert(*it); |
| 5478 | } else { |
| 5479 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5480 | if (pair.second.count(*it) != 0) { |
| 5481 | formats.insert(pair.first); |
| 5482 | break; |
| 5483 | } |
| 5484 | } |
| 5485 | } |
| 5486 | } |
| 5487 | } |
| 5488 | *numSurroundFormats = formats.size(); |
| 5489 | for (const auto& format: formats) { |
| 5490 | if (formatsWritten < formatsMax) { |
| 5491 | surroundFormats[formatsWritten++] = format; |
| 5492 | } |
| 5493 | } |
| 5494 | return NO_ERROR; |
| 5495 | } |
| 5496 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5497 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 5498 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5499 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5500 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 5501 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5502 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5503 | return BAD_VALUE; |
| 5504 | } |
| 5505 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5506 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 5507 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5508 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5509 | return INVALID_OPERATION; |
| 5510 | } |
| 5511 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5512 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5513 | return NO_ERROR; |
| 5514 | } |
| 5515 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5516 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5517 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5518 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5519 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5520 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5521 | } |
| 5522 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5523 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5524 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5525 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5526 | } |
| 5527 | } |
| 5528 | |
| 5529 | sp<SwAudioOutputDescriptor> outputDesc; |
| 5530 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5531 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 5532 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5533 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 5534 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5535 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5536 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5537 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5538 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5539 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5540 | name.c_str(), |
| 5541 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5542 | if (status != NO_ERROR) { |
| 5543 | continue; |
| 5544 | } |
| 5545 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5546 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5547 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5548 | name.c_str(), |
| 5549 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5550 | profileUpdated |= (status == NO_ERROR); |
| 5551 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5552 | // 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] | 5553 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5554 | AUDIO_DEVICE_IN_HDMI); |
| 5555 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 5556 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5557 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5558 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5559 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5560 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5561 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5562 | name.c_str(), |
| 5563 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5564 | if (status != NO_ERROR) { |
| 5565 | continue; |
| 5566 | } |
| 5567 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5568 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5569 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5570 | name.c_str(), |
| 5571 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5572 | profileUpdated |= (status == NO_ERROR); |
| 5573 | } |
| 5574 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5575 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5576 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5577 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 5581 | } |
| 5582 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5583 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5584 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5585 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 5586 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5587 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5588 | } |
| 5589 | } |
| 5590 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 5591 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 5592 | { |
| 5593 | for (const auto& hwModule : mHwModules) { |
| 5594 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5595 | for (const auto &outProfile : outputProfiles) { |
| 5596 | struct audio_port audioPort; |
| 5597 | outProfile->toAudioPort(&audioPort); |
| 5598 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 5599 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5600 | return true; |
| 5601 | } |
| 5602 | } |
| 5603 | } |
| 5604 | } |
| 5605 | return false; |
| 5606 | } |
| 5607 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 5608 | bool AudioPolicyManager::isUltrasoundSupported() |
| 5609 | { |
| 5610 | bool hasUltrasoundOutput = false; |
| 5611 | bool hasUltrasoundInput = false; |
| 5612 | for (const auto& hwModule : mHwModules) { |
| 5613 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5614 | if (!hasUltrasoundOutput) { |
| 5615 | for (const auto &outProfile : outputProfiles) { |
| 5616 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 5617 | hasUltrasoundOutput = true; |
| 5618 | break; |
| 5619 | } |
| 5620 | } |
| 5621 | } |
| 5622 | |
| 5623 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5624 | if (!hasUltrasoundInput) { |
| 5625 | for (const auto &inputProfile : inputProfiles) { |
| 5626 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 5627 | hasUltrasoundInput = true; |
| 5628 | break; |
| 5629 | } |
| 5630 | } |
| 5631 | } |
| 5632 | |
| 5633 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 5634 | return true; |
| 5635 | } |
| 5636 | return false; |
| 5637 | } |
| 5638 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5639 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 5640 | { |
| 5641 | return getConfig().isCallScreenModeSupported(); |
| 5642 | } |
| 5643 | |
| 5644 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5645 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5646 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5647 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5648 | if (!sourceDesc->isConnected()) { |
| 5649 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 5650 | return NO_ERROR; |
| 5651 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5652 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5653 | if (swOutput != 0) { |
| 5654 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5655 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5656 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5657 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5658 | if (releaseOutput(sourceDesc->portId())) { |
| 5659 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 5660 | // no need to release audio patch here but just return NO_ERROR. |
| 5661 | return NO_ERROR; |
| 5662 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5663 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5664 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5665 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5666 | // close Hwoutput and remove from mHwOutputs |
| 5667 | } else { |
| 5668 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 5669 | } |
| 5670 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5671 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5672 | sourceDesc->disconnect(); |
| 5673 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5674 | } |
| 5675 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5676 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 5677 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5678 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5679 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5680 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5681 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5682 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5683 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 5684 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5685 | source = sourceDesc; |
| 5686 | break; |
| 5687 | } |
| 5688 | } |
| 5689 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5690 | } |
| 5691 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5692 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5693 | const audio_config_t *config, |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5694 | const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5695 | { |
| 5696 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 5697 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5698 | // 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] | 5699 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5700 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 5701 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 5702 | return false; |
| 5703 | } |
| 5704 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 5705 | return false; |
| 5706 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5707 | } |
| 5708 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5709 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5710 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5711 | if (profile == nullptr) { |
| 5712 | return false; |
| 5713 | } |
| 5714 | |
| 5715 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 5716 | // the AUDIO_CONFIG_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5717 | // 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] | 5718 | // some positional channel masks. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5719 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5720 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5721 | if (!audio_is_channel_mask_spatialized(config->channel_mask)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5722 | return false; |
| 5723 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5724 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5725 | return true; |
| 5726 | } |
| 5727 | |
| 5728 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 5729 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 5730 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5731 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 5732 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5733 | audio_attributes_t attr = client->attributes(); |
| 5734 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 5735 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5736 | audio_config_base_t clientConfig = client->config(); |
| 5737 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5738 | if (desc != mSpatializerOutput |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5739 | && canBeSpatializedInt(&attr, &config, devicesTypeAddress)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5740 | streamsToInvalidate.insert(client->stream()); |
| 5741 | } |
| 5742 | } |
| 5743 | } |
| 5744 | |
| 5745 | for (audio_stream_type_t stream : streamsToInvalidate) { |
| 5746 | mpClientInterface->invalidateStream(stream); |
| 5747 | } |
| 5748 | } |
| 5749 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5750 | |
| 5751 | bool AudioPolicyManager::isOutputOnlyAvailableRouteToSomeDevice( |
| 5752 | const sp<SwAudioOutputDescriptor>& outputDesc) { |
| 5753 | if (outputDesc->isDuplicated()) { |
| 5754 | return false; |
| 5755 | } |
| 5756 | DeviceVector devices = outputDesc->supportedDevices(); |
| 5757 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5758 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5759 | if (desc == outputDesc || desc->isDuplicated()) { |
| 5760 | continue; |
| 5761 | } |
| 5762 | DeviceVector sharedDevices = desc->filterSupportedDevices(devices); |
| 5763 | if (!sharedDevices.isEmpty() |
| 5764 | && (desc->devicesSupportEncodedFormats(sharedDevices.types()) |
| 5765 | == outputDesc->devicesSupportEncodedFormats(sharedDevices.types()))) { |
| 5766 | return false; |
| 5767 | } |
| 5768 | } |
| 5769 | return true; |
| 5770 | } |
| 5771 | |
| 5772 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5773 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5774 | const audio_attributes_t *attr, |
| 5775 | audio_io_handle_t *output) { |
| 5776 | *output = AUDIO_IO_HANDLE_NONE; |
| 5777 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5778 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 5779 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5780 | audio_config_t *configPtr = nullptr; |
| 5781 | audio_config_t config; |
| 5782 | if (mixerConfig != nullptr) { |
| 5783 | config = audio_config_initializer(mixerConfig); |
| 5784 | configPtr = &config; |
| 5785 | } |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5786 | if (!canBeSpatializedInt(attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5787 | ALOGV("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5788 | return BAD_VALUE; |
| 5789 | } |
| 5790 | |
| 5791 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5792 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5793 | if (profile == nullptr) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5794 | 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] | 5795 | return BAD_VALUE; |
| 5796 | } |
| 5797 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5798 | std::vector<sp<SwAudioOutputDescriptor>> spatializerOutputs; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5799 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5800 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5801 | if (!desc->isDuplicated() |
| 5802 | && (desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 5803 | spatializerOutputs.push_back(desc); |
| 5804 | ALOGV("%s adding opened spatializer Output %d", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5805 | } |
| 5806 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5807 | mSpatializerOutput.clear(); |
| 5808 | bool outputsChanged = false; |
| 5809 | for (const auto& desc : spatializerOutputs) { |
| 5810 | if (desc->mProfile == profile |
| 5811 | && (configPtr == nullptr |
| 5812 | || configPtr->channel_mask == desc->mMixerChannelMask)) { |
| 5813 | mSpatializerOutput = desc; |
| 5814 | ALOGV("%s reusing current spatializer output %d", __func__, desc->mIoHandle); |
| 5815 | } else { |
| 5816 | ALOGV("%s closing spatializerOutput output %d to match channel mask %#x" |
| 5817 | " and devices %s", __func__, desc->mIoHandle, |
| 5818 | configPtr != nullptr ? configPtr->channel_mask : 0, |
| 5819 | devices.toString().c_str()); |
| 5820 | closeOutput(desc->mIoHandle); |
| 5821 | outputsChanged = true; |
| 5822 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5823 | } |
| 5824 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5825 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5826 | sp<SwAudioOutputDescriptor> desc = |
| 5827 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5828 | if (desc != nullptr) { |
| 5829 | mSpatializerOutput = desc; |
| 5830 | outputsChanged = true; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5831 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5832 | } |
| 5833 | |
| 5834 | checkVirtualizerClientRoutes(); |
| 5835 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5836 | if (outputsChanged) { |
| 5837 | mPreviousOutputs = mOutputs; |
| 5838 | mpClientInterface->onAudioPortListUpdate(); |
| 5839 | } |
| 5840 | |
| 5841 | if (mSpatializerOutput == nullptr) { |
| 5842 | ALOGV("%s could not open spatializer output with requested config", __func__); |
| 5843 | return BAD_VALUE; |
| 5844 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5845 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5846 | ALOGV("%s returning new spatializer output %d", __func__, *output); |
| 5847 | return OK; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5848 | } |
| 5849 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5850 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 5851 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5852 | return INVALID_OPERATION; |
| 5853 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5854 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5855 | return BAD_VALUE; |
| 5856 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5857 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5858 | if (!isOutputOnlyAvailableRouteToSomeDevice(mSpatializerOutput)) { |
| 5859 | ALOGV("%s closing spatializer output %d", __func__, mSpatializerOutput->mIoHandle); |
| 5860 | closeOutput(mSpatializerOutput->mIoHandle); |
| 5861 | //from now on mSpatializerOutput is null |
| 5862 | checkVirtualizerClientRoutes(); |
| 5863 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5864 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5865 | return NO_ERROR; |
| 5866 | } |
| 5867 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5868 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5869 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5870 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5871 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 5872 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 5873 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5874 | } |
| 5875 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5876 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5877 | if (std::string audioPolicyXmlConfigFile = audio_get_audio_policy_config_file(); |
| 5878 | !audioPolicyXmlConfigFile.empty()) { |
| 5879 | status_t ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile.c_str(), &config); |
| 5880 | if (ret == NO_ERROR) { |
| 5881 | config.setSource(audioPolicyXmlConfigFile); |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 5882 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5883 | return ret; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 5884 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5885 | return BAD_VALUE; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5886 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5887 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5888 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 5889 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5890 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5891 | mUidCached(AID_AUDIOSERVER), // no need to call getuid(), there's only one of us running. |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5892 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5893 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5894 | mA2dpSuspended(false), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5895 | mConfig(mHwModulesAll, mOutputDevicesAll, mInputDevicesAll, mDefaultOutputDevice), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5896 | mAudioPortGeneration(1), |
| 5897 | mBeaconMuteRefCount(0), |
| 5898 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5899 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5900 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5901 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5902 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5903 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5904 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5905 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5906 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 5907 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 5908 | { |
| 5909 | loadConfig(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5910 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5911 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 5912 | void AudioPolicyManager::loadConfig() { |
| 5913 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5914 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5915 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5916 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 5920 | { |
| 5921 | auto engLib = EngineLibrary::load( |
| 5922 | "libaudiopolicyengine" + getConfig().getEngineLibraryNameSuffix() + ".so"); |
| 5923 | if (!engLib) { |
| 5924 | ALOGE("%s: Failed to load the engine library", __FUNCTION__); |
| 5925 | return NO_INIT; |
| 5926 | } |
| 5927 | mEngine = engLib->createEngine(); |
| 5928 | if (mEngine == nullptr) { |
| 5929 | ALOGE("%s: Failed to instantiate the APM engine", __FUNCTION__); |
| 5930 | return NO_INIT; |
| 5931 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5932 | } |
| 5933 | mEngine->setObserver(this); |
| 5934 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5935 | if (status != NO_ERROR) { |
| 5936 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 5937 | return status; |
| 5938 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5939 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 5940 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 5941 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 5942 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 5943 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5944 | // after parsing the config, mOutputDevicesAll and mInputDevicesAll contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5945 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5946 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5947 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5948 | // make sure default device is reachable |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5949 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { |
| 5950 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", |
| 5951 | mDefaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5952 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5953 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5954 | // If microphones address is empty, set it according to device type |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 5955 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5956 | if (mAvailableInputDevices[i]->address().empty()) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5957 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5958 | mAvailableInputDevices[i]->setAddress(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5959 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5960 | mAvailableInputDevices[i]->setAddress(AUDIO_BACK_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5961 | } |
| 5962 | } |
| 5963 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5964 | |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5965 | 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] | 5966 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 5967 | // Silence ALOGV statements |
| 5968 | property_set("log.tag." LOG_TAG, "D"); |
| 5969 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5970 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5971 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5972 | } |
| 5973 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5974 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5975 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5976 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5977 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5978 | } |
| 5979 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5980 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5981 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5982 | mAvailableOutputDevices.clear(); |
| 5983 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5984 | mOutputs.clear(); |
| 5985 | mInputs.clear(); |
| 5986 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 5987 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5988 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5989 | } |
| 5990 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5991 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5992 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5993 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5994 | } |
| 5995 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5996 | // --- |
| 5997 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5998 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 5999 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6000 | DeviceVector newDevices; |
| 6001 | onNewAudioModulesAvailableInt(&newDevices); |
| 6002 | if (!newDevices.empty()) { |
| 6003 | nextAudioPortGeneration(); |
| 6004 | mpClientInterface->onAudioPortListUpdate(); |
| 6005 | } |
| 6006 | } |
| 6007 | |
| 6008 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 6009 | { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6010 | for (const auto& hwModule : mHwModulesAll) { |
| 6011 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 6012 | continue; |
| 6013 | } |
| 6014 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
| 6015 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 6016 | ALOGW("could not open HW module %s", hwModule->getName()); |
| 6017 | continue; |
| 6018 | } |
| 6019 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 6020 | // open all output streams needed to access attached devices. |
| 6021 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6022 | // This also validates mAvailableOutputDevices list |
| 6023 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 6024 | if (!outProfile->canOpenNewIo()) { |
| 6025 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 6026 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 6027 | continue; |
| 6028 | } |
| 6029 | if (!outProfile->hasSupportedDevices()) { |
| 6030 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 6031 | continue; |
| 6032 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 6033 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 6034 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6035 | mTtsOutputAvailable = true; |
| 6036 | } |
| 6037 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6038 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 6039 | DeviceVector availProfileDevices = supportedDevices.filter(mOutputDevicesAll); |
| 6040 | sp<DeviceDescriptor> supportedDevice = 0; |
| 6041 | if (supportedDevices.contains(mDefaultOutputDevice)) { |
| 6042 | supportedDevice = mDefaultOutputDevice; |
| 6043 | } else { |
| 6044 | // choose first device present in profile's SupportedDevices also part of |
| 6045 | // mAvailableOutputDevices. |
| 6046 | if (availProfileDevices.isEmpty()) { |
| 6047 | continue; |
| 6048 | } |
| 6049 | supportedDevice = availProfileDevices.itemAt(0); |
| 6050 | } |
| 6051 | if (!mOutputDevicesAll.contains(supportedDevice)) { |
| 6052 | continue; |
| 6053 | } |
| 6054 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 6055 | mpClientInterface); |
| 6056 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 6057 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 6058 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6059 | AUDIO_STREAM_DEFAULT, |
| 6060 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 6061 | if (status != NO_ERROR) { |
| 6062 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 6063 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 6064 | continue; |
| 6065 | } |
| 6066 | for (const auto &device : availProfileDevices) { |
| 6067 | // give a valid ID to an attached device once confirmed it is reachable |
| 6068 | if (!device->isAttached()) { |
| 6069 | device->attach(hwModule); |
| 6070 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 6071 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6072 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6073 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6074 | } |
| 6075 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6076 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6077 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 6078 | mPrimaryOutput = outputDesc; |
| 6079 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 6080 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6081 | outputDesc->close(); |
| 6082 | } else { |
| 6083 | addOutput(output, outputDesc); |
| 6084 | setOutputDevices(outputDesc, |
| 6085 | DeviceVector(supportedDevice), |
| 6086 | true, |
| 6087 | 0, |
| 6088 | NULL); |
| 6089 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6090 | } |
| 6091 | // open input streams needed to access attached devices to validate |
| 6092 | // mAvailableInputDevices list |
| 6093 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 6094 | if (!inProfile->canOpenNewIo()) { |
| 6095 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 6096 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 6097 | continue; |
| 6098 | } |
| 6099 | if (!inProfile->hasSupportedDevices()) { |
| 6100 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 6101 | continue; |
| 6102 | } |
| 6103 | // chose first device present in profile's SupportedDevices also part of |
| 6104 | // available input devices |
| 6105 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 6106 | DeviceVector availProfileDevices = supportedDevices.filter(mInputDevicesAll); |
| 6107 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6108 | ALOGV("%s: Input device list is empty! for profile %s", |
| 6109 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6110 | continue; |
| 6111 | } |
| 6112 | sp<AudioInputDescriptor> inputDesc = |
| 6113 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 6114 | |
| 6115 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 6116 | status_t status = inputDesc->open(nullptr, |
| 6117 | availProfileDevices.itemAt(0), |
| 6118 | AUDIO_SOURCE_MIC, |
| 6119 | AUDIO_INPUT_FLAG_NONE, |
| 6120 | &input); |
| 6121 | if (status != NO_ERROR) { |
| 6122 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 6123 | availProfileDevices.toString().c_str(), |
| 6124 | hwModule->getName()); |
| 6125 | continue; |
| 6126 | } |
| 6127 | for (const auto &device : availProfileDevices) { |
| 6128 | // give a valid ID to an attached device once confirmed it is reachable |
| 6129 | if (!device->isAttached()) { |
| 6130 | device->attach(hwModule); |
| 6131 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 6132 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 6133 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6134 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 6135 | } |
| 6136 | } |
| 6137 | inputDesc->close(); |
| 6138 | } |
| 6139 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 6140 | |
| 6141 | // Check if spatializer outputs can be closed until used. |
| 6142 | // mOutputs vector never contains duplicated outputs at this point. |
| 6143 | std::vector<audio_io_handle_t> outputsClosed; |
| 6144 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6145 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 6146 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0 |
| 6147 | && !isOutputOnlyAvailableRouteToSomeDevice(desc)) { |
| 6148 | outputsClosed.push_back(desc->mIoHandle); |
| 6149 | desc->close(); |
| 6150 | } |
| 6151 | } |
| 6152 | for (auto output : outputsClosed) { |
| 6153 | removeOutput(output); |
| 6154 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6157 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 6158 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6159 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6160 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6161 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 6162 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6163 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6164 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6165 | } |
| 6166 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6167 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 6168 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6169 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 6170 | ALOGV("%s: removing primary output", __func__); |
| 6171 | mPrimaryOutput = nullptr; |
| 6172 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6173 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6174 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6175 | } |
| 6176 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 6177 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 6178 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6179 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6180 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6181 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6182 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6183 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6184 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6185 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6186 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6187 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6188 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6189 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6190 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6191 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6192 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6193 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6194 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6195 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6196 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6197 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 6198 | // first call getAudioPort to get the supported attributes from the HAL |
| 6199 | struct audio_port_v7 port = {}; |
| 6200 | device->toAudioPort(&port); |
| 6201 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6202 | if (status == NO_ERROR) { |
| 6203 | device->importAudioPort(port); |
| 6204 | } |
| 6205 | |
| 6206 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6207 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6208 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6209 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6210 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6211 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 6212 | mOutputs.keyAt(i), device->toString().c_str()); |
| 6213 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6214 | } |
| 6215 | } |
| 6216 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6217 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6218 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6219 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6220 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6221 | if (profile->supportsDevice(device)) { |
| 6222 | profiles.add(profile); |
| 6223 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 6224 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6225 | } |
| 6226 | } |
| 6227 | } |
| 6228 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 6229 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6230 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6231 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6232 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6233 | return BAD_VALUE; |
| 6234 | } |
| 6235 | |
| 6236 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 6237 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6238 | 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] | 6239 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6240 | |
| 6241 | // nothing to do if one output is already opened for this profile |
| 6242 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6243 | for (j = 0; j < outputs.size(); j++) { |
| 6244 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6245 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6246 | // matching profile: save the sample rates, format and channel masks supported |
| 6247 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6248 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6249 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6250 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6251 | break; |
| 6252 | } |
| 6253 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6254 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6255 | continue; |
| 6256 | } |
| 6257 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6258 | if (!profile->canOpenNewIo()) { |
| 6259 | ALOGW("Max Output number %u already opened for this profile %s", |
| 6260 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6261 | continue; |
| 6262 | } |
| 6263 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 6264 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 6265 | deviceType, address.string(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6266 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 6267 | 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] | 6268 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6269 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6270 | profiles.removeAt(profile_index); |
| 6271 | profile_index--; |
| 6272 | } else { |
| 6273 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6274 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6275 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6276 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 6277 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6278 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6279 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 6280 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6281 | if (device_distinguishes_on_address(deviceType)) { |
| 6282 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 6283 | device->toString().c_str()); |
| 6284 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 6285 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6286 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6287 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 6288 | } |
| 6289 | } |
| 6290 | |
| 6291 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6292 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6293 | return BAD_VALUE; |
| 6294 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6295 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6296 | // check if one opened output is not needed any more after disconnecting one device |
| 6297 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6298 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6299 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6300 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6301 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 6302 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6303 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6304 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6305 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 6306 | mOutputs.keyAt(i)); |
| 6307 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 6308 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6309 | } |
| 6310 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6311 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6312 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6313 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 6314 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6315 | if (!profile->supportsDevice(device)) { |
| 6316 | continue; |
| 6317 | } |
| 6318 | ALOGV("checkOutputsForDevice(): " |
| 6319 | "clearing direct output profile %zu on module %s", |
| 6320 | j, hwModule->getName()); |
| 6321 | profile->clearAudioProfiles(); |
| 6322 | if (!profile->hasDynamicAudioProfile()) { |
| 6323 | continue; |
| 6324 | } |
| 6325 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 6326 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 6327 | // the capabilities of the devices that is supported by the IOProfile. |
| 6328 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 6329 | if (supportedDevice == device || |
| 6330 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 6331 | continue; |
| 6332 | } |
| 6333 | struct audio_port_v7 port; |
| 6334 | supportedDevice->toAudioPort(&port); |
| 6335 | status_t status = mpClientInterface->getAudioPort(&port); |
| 6336 | if (status == NO_ERROR) { |
| 6337 | supportedDevice->importAudioPort(port); |
| 6338 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6339 | } |
| 6340 | } |
| 6341 | } |
| 6342 | } |
| 6343 | return NO_ERROR; |
| 6344 | } |
| 6345 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6346 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6347 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6348 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6349 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6350 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6351 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6352 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6353 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6354 | } |
| 6355 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6356 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6357 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6358 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6359 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6360 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6361 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6362 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6363 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6364 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6365 | if (profile->supportsDevice(device)) { |
| 6366 | profiles.add(profile); |
| 6367 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 6368 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6373 | if (profiles.isEmpty()) { |
| 6374 | ALOGW("%s: No input profile available for device %s", |
| 6375 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6376 | return BAD_VALUE; |
| 6377 | } |
| 6378 | |
| 6379 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 6380 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6381 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 6382 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6383 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6384 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6385 | // nothing to do if one input is already opened for this profile |
| 6386 | size_t input_index; |
| 6387 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 6388 | desc = mInputs.valueAt(input_index); |
| 6389 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6390 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6391 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6392 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6393 | break; |
| 6394 | } |
| 6395 | } |
| 6396 | if (input_index != mInputs.size()) { |
| 6397 | continue; |
| 6398 | } |
| 6399 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6400 | if (!profile->canOpenNewIo()) { |
| 6401 | ALOGW("Max Input number %u already opened for this profile %s", |
| 6402 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6403 | continue; |
| 6404 | } |
| 6405 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6406 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6407 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6408 | status_t status = desc->open(nullptr, |
| 6409 | device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6410 | AUDIO_SOURCE_MIC, |
| 6411 | AUDIO_INPUT_FLAG_NONE, |
| 6412 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6413 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6414 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6415 | const String8& address = String8(device->address().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6416 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6417 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6418 | mpClientInterface->setParameters(input, String8(param)); |
| 6419 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6420 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6421 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6422 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6423 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6424 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6425 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6426 | } |
| 6427 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6428 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6429 | addInput(input, desc); |
| 6430 | } |
| 6431 | } // endif input != 0 |
| 6432 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6433 | if (input == AUDIO_IO_HANDLE_NONE) { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 6434 | ALOGW("%s could not open input for device %s", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6435 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6436 | profiles.removeAt(profile_index); |
| 6437 | profile_index--; |
| 6438 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6439 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6440 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6441 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6442 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 6443 | } |
| 6444 | } // end scan profiles |
| 6445 | |
| 6446 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6447 | 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] | 6448 | return BAD_VALUE; |
| 6449 | } |
| 6450 | } else { |
| 6451 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6452 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6453 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6454 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6455 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6456 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6457 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6458 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6459 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 6460 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6461 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6462 | } |
| 6463 | } |
| 6464 | } |
| 6465 | } // end disconnect |
| 6466 | |
| 6467 | return NO_ERROR; |
| 6468 | } |
| 6469 | |
| 6470 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6471 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6472 | { |
| 6473 | ALOGV("closeOutput(%d)", output); |
| 6474 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6475 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 6476 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6477 | ALOGW("closeOutput() unknown output %d", output); |
| 6478 | return; |
| 6479 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6480 | const bool closingOutputWasActive = closingOutput->isActive(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6481 | mPolicyMixes.closeOutput(closingOutput); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6482 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6483 | // look for duplicated outputs connected to the output being removed. |
| 6484 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6485 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 6486 | if (dupOutput->isDuplicated() && |
| 6487 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 6488 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 6489 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6490 | // As all active tracks on duplicated output will be deleted, |
| 6491 | // and as they were also referenced on the other output, the reference |
| 6492 | // count for their stream type must be adjusted accordingly on |
| 6493 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6494 | const bool wasActive = remainingOutput->isActive(); |
| 6495 | // Note: no-op on the closing output where all clients has already been set inactive |
| 6496 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6497 | // stop() will be a no op if the output is still active but is needed in case all |
| 6498 | // active streams refcounts where cleared above |
| 6499 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6500 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6501 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6502 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 6503 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 6504 | |
| 6505 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6506 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6507 | } |
| 6508 | } |
| 6509 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6510 | nextAudioPortGeneration(); |
| 6511 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6512 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6513 | if (index >= 0) { |
| 6514 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6515 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6516 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6517 | mAudioPatches.removeItemsAt(index); |
| 6518 | mpClientInterface->onAudioPatchListUpdate(); |
| 6519 | } |
| 6520 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6521 | if (closingOutputWasActive) { |
| 6522 | closingOutput->stop(); |
| 6523 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6524 | closingOutput->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6525 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6526 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6527 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6528 | if (closingOutput == mSpatializerOutput) { |
| 6529 | mSpatializerOutput.clear(); |
| 6530 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6531 | |
| 6532 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 6533 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6534 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6535 | bool directOutputOpen = false; |
| 6536 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6537 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 6538 | directOutputOpen = true; |
| 6539 | break; |
| 6540 | } |
| 6541 | } |
| 6542 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6543 | ALOGV("no direct outputs open, reset MSD patches"); |
| 6544 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 6545 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 6546 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 6547 | // devices to patch to. This may be complicated by the fact that devices may become |
| 6548 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6549 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6550 | } |
| 6551 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 6555 | { |
| 6556 | ALOGV("closeInput(%d)", input); |
| 6557 | |
| 6558 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 6559 | if (inputDesc == NULL) { |
| 6560 | ALOGW("closeInput() unknown input %d", input); |
| 6561 | return; |
| 6562 | } |
| 6563 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6564 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6565 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6566 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6567 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6568 | if (index >= 0) { |
| 6569 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6570 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6571 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6572 | mAudioPatches.removeItemsAt(index); |
| 6573 | mpClientInterface->onAudioPatchListUpdate(); |
| 6574 | } |
| 6575 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6576 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6577 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6578 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6579 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 6580 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6581 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 6582 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6583 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6584 | } |
| 6585 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6586 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 6587 | const DeviceVector &devices, |
| 6588 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6589 | { |
| 6590 | SortedVector<audio_io_handle_t> outputs; |
| 6591 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6592 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6593 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6594 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 6595 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6596 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6597 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6598 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6599 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6600 | outputs.add(openOutputs.keyAt(i)); |
| 6601 | } |
| 6602 | } |
| 6603 | return outputs; |
| 6604 | } |
| 6605 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6606 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 6607 | { |
| 6608 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 6609 | // output is suspended before any tracks are moved to it |
| 6610 | checkA2dpSuspend(); |
| 6611 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6612 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6613 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6614 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 6615 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6616 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 6617 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 6618 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 6619 | // unnecessary rerouting by caching and reusing the arguments to |
| 6620 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 6621 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6622 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6623 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 6624 | // an event that changed routing likely occurred, inform upper layers |
| 6625 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6626 | } |
| 6627 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6628 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 6629 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6630 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6631 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 6632 | mEngine->getProductStrategyForAttributes(rAttr); |
| 6633 | } |
| 6634 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6635 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 6636 | { |
| 6637 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6638 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6639 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6640 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6641 | && !isCallRxAudioSource(sourceDesc) && !sourceDesc->isInternal()) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6642 | connectAudioSource(sourceDesc); |
| 6643 | } |
| 6644 | } |
| 6645 | } |
| 6646 | |
| 6647 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 6648 | { |
| 6649 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6650 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6651 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 6652 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 6653 | disconnectAudioSource(sourceDesc); |
| 6654 | } |
| 6655 | } |
| 6656 | } |
| 6657 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6658 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 6659 | { |
| 6660 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 6661 | |
| 6662 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 6663 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 6664 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6665 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 6666 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6667 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6668 | uint32_t maxLatency = 0; |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6669 | std::vector<sp<SwAudioOutputDescriptor>> invalidatedOutputs; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6670 | // take into account dynamic audio policies related changes: if a client is now associated |
| 6671 | // to a different policy mix than at creation time, invalidate corresponding stream |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6672 | for (size_t i = 0; i < mPreviousOutputs.size(); i++) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6673 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 6674 | if (desc->isDuplicated()) { |
| 6675 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6676 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6677 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 6678 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 6679 | continue; |
| 6680 | } |
| 6681 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6682 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 6683 | client->uid(), client->session(), client->flags(), primaryMix, nullptr); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6684 | if (status != OK) { |
| 6685 | continue; |
| 6686 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 6687 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6688 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6689 | maxLatency = desc->latency(); |
| 6690 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6691 | invalidatedOutputs.push_back(desc); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6692 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6693 | } |
| 6694 | } |
| 6695 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6696 | if (srcOutputs != dstOutputs || !invalidatedOutputs.empty()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6697 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 6698 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6699 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6700 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6701 | if (desc == nullptr) continue; |
| 6702 | |
| 6703 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6704 | maxLatency = desc->latency(); |
| 6705 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6706 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6707 | bool invalidate = false; |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6708 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 6709 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6710 | // a client on a non direct outputs has necessarily a linear PCM format |
| 6711 | // so we can call selectOutput() safely |
| 6712 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 6713 | client->flags(), |
| 6714 | client->config().format, |
| 6715 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 6716 | client->config().sample_rate, |
| 6717 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6718 | if (newOutput != srcOut) { |
| 6719 | invalidate = true; |
| 6720 | break; |
| 6721 | } |
| 6722 | } else { |
| 6723 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 6724 | client->config().sample_rate, |
| 6725 | client->config().format, |
| 6726 | client->config().channel_mask, |
| 6727 | client->flags(), |
| 6728 | true /* directOnly */); |
| 6729 | if (profile != desc->mProfile) { |
| 6730 | invalidate = true; |
| 6731 | break; |
| 6732 | } |
| 6733 | } |
| 6734 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6735 | // mute strategy while moving tracks from one output to another |
| 6736 | if (invalidate) { |
| 6737 | invalidatedOutputs.push_back(desc); |
| 6738 | if (desc->isStrategyActive(psId)) { |
| 6739 | setStrategyMute(psId, true, desc); |
| 6740 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 6741 | newDevices.types()); |
| 6742 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6743 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6744 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6745 | if (source != nullptr && !isCallRxAudioSource(source) && !source->isInternal()) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6746 | connectAudioSource(source); |
| 6747 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6748 | } |
| 6749 | |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6750 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
| 6751 | "%s: strategy %d, moving from output %s to output %s", __func__, psId, |
| 6752 | std::to_string(srcOutputs[0]).c_str(), |
| 6753 | std::to_string(dstOutputs[0]).c_str()); |
| 6754 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6755 | // Move effects associated to this stream from previous output to new output |
| 6756 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6757 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6758 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6759 | // 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] | 6760 | if (!invalidatedOutputs.empty()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6761 | for (auto stream : mEngine->getStreamTypesForProductStrategy(psId)) { |
| 6762 | mpClientInterface->invalidateStream(stream); |
| 6763 | } |
Eric Laurent | 56ed884 | 2022-11-15 16:04:41 +0100 | [diff] [blame] | 6764 | for (sp<SwAudioOutputDescriptor> desc : invalidatedOutputs) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 6765 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 6766 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6767 | } |
| 6768 | } |
| 6769 | } |
| 6770 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6771 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6772 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6773 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 6774 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 6775 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6776 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6777 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6778 | } |
| 6779 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6780 | void AudioPolicyManager::checkSecondaryOutputs() { |
| 6781 | std::set<audio_stream_type_t> streamsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6782 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6783 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6784 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 6785 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6786 | sp<AudioPolicyMix> primaryMix; |
| 6787 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6788 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 6789 | client->uid(), client->session(), client->flags(), primaryMix, &secondaryMixes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6790 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 6791 | for (auto &secondaryMix : secondaryMixes) { |
| 6792 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 6793 | if (outputDesc != nullptr && |
| 6794 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 6795 | secondaryDescs.push_back(outputDesc); |
| 6796 | } |
| 6797 | } |
| 6798 | |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6799 | if (status != OK) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6800 | streamsToInvalidate.insert(client->stream()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6801 | } else if (!std::equal( |
| 6802 | client->getSecondaryOutputs().begin(), |
| 6803 | client->getSecondaryOutputs().end(), |
| 6804 | secondaryDescs.begin(), secondaryDescs.end())) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6805 | if (!audio_is_linear_pcm(client->config().format)) { |
| 6806 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 6807 | // behavior when the secondary output is changed. |
| 6808 | streamsToInvalidate.insert(client->stream()); |
| 6809 | } else { |
| 6810 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 6811 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 6812 | for (const auto &secondaryDesc: secondaryDescs) { |
| 6813 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 6814 | weakSecondaryDescs.push_back(secondaryDesc); |
| 6815 | } |
| 6816 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 6817 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6818 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6819 | } |
| 6820 | } |
| 6821 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6822 | if (!trackSecondaryOutputs.empty()) { |
| 6823 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 6824 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6825 | for (audio_stream_type_t stream : streamsToInvalidate) { |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6826 | ALOGD("%s Invalidate stream %d due to fail getting output for attr", __func__, stream); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6827 | mpClientInterface->invalidateStream(stream); |
| 6828 | } |
| 6829 | } |
| 6830 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6831 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 6832 | AudioDeviceTypeAddrVector devices; |
| 6833 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 6834 | for (const auto &device : devices) { |
| 6835 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 6836 | return true; |
| 6837 | } |
| 6838 | } |
| 6839 | return false; |
| 6840 | } |
| 6841 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 6842 | bool AudioPolicyManager::isHearingAidUsedForComm() const { |
| 6843 | DeviceVector devices = mEngine->getOutputDevicesForStream(AUDIO_STREAM_VOICE_CALL, |
| 6844 | true /*fromCache*/); |
| 6845 | for (const auto &device : devices) { |
| 6846 | if (device->type() == AUDIO_DEVICE_OUT_HEARING_AID) { |
| 6847 | return true; |
| 6848 | } |
| 6849 | } |
| 6850 | return false; |
| 6851 | } |
| 6852 | |
| 6853 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6854 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6855 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6856 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 6857 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6858 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6859 | return; |
| 6860 | } |
| 6861 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6862 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6863 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 6864 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6865 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6866 | |
| 6867 | // if suspended, restore A2DP output if: |
| 6868 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6869 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6870 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6871 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6872 | // if not suspended, suspend A2DP output if: |
| 6873 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6874 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6875 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6876 | // |
| 6877 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6878 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6879 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6880 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6881 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6882 | |
| 6883 | mpClientInterface->restoreOutput(a2dpOutput); |
| 6884 | mA2dpSuspended = false; |
| 6885 | } |
| 6886 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6887 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6888 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6889 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6890 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6891 | |
| 6892 | mpClientInterface->suspendOutput(a2dpOutput); |
| 6893 | mA2dpSuspended = true; |
| 6894 | } |
| 6895 | } |
| 6896 | } |
| 6897 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6898 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 6899 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6900 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6901 | DeviceVector devices; |
| 6902 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 6903 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6904 | if (index >= 0) { |
| 6905 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6906 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6907 | ALOGV("%s device %s forced by patch %d", __func__, |
| 6908 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 6909 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6910 | } |
| 6911 | } |
| 6912 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 6913 | // Do not retrieve engine device for outputs through MSD |
| 6914 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 6915 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 6916 | return outputDesc->devices(); |
| 6917 | } |
| 6918 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6919 | // Honor explicit routing requests only if no client using default routing is active on this |
| 6920 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 6921 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6922 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6923 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6924 | if (device != nullptr) { |
| 6925 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 6926 | } |
| 6927 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 6928 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 6929 | // of setForceUse / Default Bus device here |
| 6930 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 6931 | if (device != nullptr) { |
| 6932 | return DeviceVector(device); |
| 6933 | } |
| 6934 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6935 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 6936 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
| 6937 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6938 | auto hasStreamActive = [&](auto stream) { |
| 6939 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 6940 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 6941 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6942 | auto doGetOutputDevicesForVoice = [&]() { |
| 6943 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 6944 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6945 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 6946 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 6947 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6948 | }; |
| 6949 | |
| 6950 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 6951 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 6952 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 6953 | // 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] | 6954 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 6955 | // stream is associated to the output descriptor. |
| 6956 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 6957 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 6958 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 6959 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6960 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 6961 | // check the route (would force modifying configuration file for this profile) |
| 6962 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 6963 | break; |
| 6964 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6965 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6966 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6967 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6968 | } |
| 6969 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6970 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 6971 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6972 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6973 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6974 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6975 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6976 | if (index >= 0) { |
| 6977 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6978 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6979 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 6980 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 6981 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6982 | } |
| 6983 | } |
| 6984 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6985 | // Honor explicit routing requests only if no client using default routing is active on this |
| 6986 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 6987 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6988 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 6989 | if (device != nullptr) { |
| 6990 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6991 | } |
| 6992 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 6993 | // 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] | 6994 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6995 | audio_attributes_t attributes; |
| 6996 | uid_t uid; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 6997 | audio_session_t session; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6998 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 6999 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7000 | attributes = topClient->attributes(); |
| 7001 | uid = topClient->uid(); |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7002 | session = topClient->session(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7003 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7004 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 7005 | uid = 0; |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7006 | session = AUDIO_SESSION_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 7007 | } |
| 7008 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7009 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 7010 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 7011 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7012 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 7013 | device = mEngine->getInputDeviceForAttributes(attributes, uid, session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 7014 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7015 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7016 | return device; |
| 7017 | } |
| 7018 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 7019 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 7020 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 7021 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 7022 | } |
| 7023 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7024 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7025 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7026 | if (devices == nullptr) { |
| 7027 | return BAD_VALUE; |
| 7028 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7029 | |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7030 | DeviceVector curDevices; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 7031 | if (status_t status = getDevicesForAttributes(attr, curDevices, forVolume); status != OK) { |
| 7032 | return status; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 7033 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 7034 | for (const auto& device : curDevices) { |
| 7035 | devices->push_back(device->getDeviceTypeAddr()); |
| 7036 | } |
| 7037 | return NO_ERROR; |
| 7038 | } |
| 7039 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7040 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7041 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7042 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7043 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7044 | updateDevicesAndOutputs(); |
| 7045 | break; |
| 7046 | default: |
| 7047 | break; |
| 7048 | } |
| 7049 | } |
| 7050 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7051 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 7052 | |
| 7053 | // skip beacon mute management if a dedicated TTS output is available |
| 7054 | if (mTtsOutputAvailable) { |
| 7055 | return 0; |
| 7056 | } |
| 7057 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7058 | switch(event) { |
| 7059 | case STARTING_OUTPUT: |
| 7060 | mBeaconMuteRefCount++; |
| 7061 | break; |
| 7062 | case STOPPING_OUTPUT: |
| 7063 | if (mBeaconMuteRefCount > 0) { |
| 7064 | mBeaconMuteRefCount--; |
| 7065 | } |
| 7066 | break; |
| 7067 | case STARTING_BEACON: |
| 7068 | mBeaconPlayingRefCount++; |
| 7069 | break; |
| 7070 | case STOPPING_BEACON: |
| 7071 | if (mBeaconPlayingRefCount > 0) { |
| 7072 | mBeaconPlayingRefCount--; |
| 7073 | } |
| 7074 | break; |
| 7075 | } |
| 7076 | |
| 7077 | if (mBeaconMuteRefCount > 0) { |
| 7078 | // any playback causes beacon to be muted |
| 7079 | return setBeaconMute(true); |
| 7080 | } else { |
| 7081 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 7082 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 7087 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 7088 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 7089 | // keep track of muted state to avoid repeating mute/unmute operations |
| 7090 | if (mBeaconMuted != mute) { |
| 7091 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 7092 | ALOGV("\t muting %d", mute); |
| 7093 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7094 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 7095 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 7096 | ALOGV("\t no tts volume source available"); |
| 7097 | return 0; |
| 7098 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7099 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7100 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7101 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7102 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 7103 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 7104 | maxLatency = latency; |
| 7105 | } |
| 7106 | } |
| 7107 | mBeaconMuted = mute; |
| 7108 | return maxLatency; |
| 7109 | } |
| 7110 | return 0; |
| 7111 | } |
| 7112 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 7113 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7114 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7115 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7116 | mPreviousOutputs = mOutputs; |
| 7117 | } |
| 7118 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 7119 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7120 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7121 | uint32_t delayMs) |
| 7122 | { |
| 7123 | // mute/unmute strategies using an incompatible device combination |
| 7124 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 7125 | // if unmuting, unmute only after the specified delay |
| 7126 | if (outputDesc->isDuplicated()) { |
| 7127 | return 0; |
| 7128 | } |
| 7129 | |
| 7130 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7131 | DeviceVector devices = outputDesc->devices(); |
| 7132 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7133 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7134 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 7135 | for (const auto &productStrategy : productStrategies) { |
| 7136 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 7137 | DeviceVector curDevices = |
| 7138 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 7139 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 7140 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7141 | bool doMute = false; |
| 7142 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7143 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7144 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7145 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 7146 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7147 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7148 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7149 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7150 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7151 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7152 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7153 | // 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] | 7154 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7155 | continue; |
| 7156 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7157 | ALOGVV("%s() %s (curDevice %s)", __func__, |
| 7158 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 7159 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 7160 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7161 | if (mute) { |
| 7162 | // FIXME: should not need to double latency if volume could be applied |
| 7163 | // immediately by the audioflinger mixer. We must account for the delay |
| 7164 | // between now and the next time the audioflinger thread for this output |
| 7165 | // will process a buffer (which corresponds to one buffer size, |
| 7166 | // usually 1/2 or 1/4 of the latency). |
| 7167 | if (muteWaitMs < desc->latency() * 2) { |
| 7168 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7169 | } |
| 7170 | } |
| 7171 | } |
| 7172 | } |
| 7173 | } |
| 7174 | } |
| 7175 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7176 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 7177 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7178 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7179 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7180 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 7181 | if (muteWaitMs < tempMuteWaitMs) { |
| 7182 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7183 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 7184 | |
| 7185 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 7186 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 7187 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 7188 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 7189 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 7190 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 7191 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7192 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 7193 | // make sure that we do not start the temporary mute period too early in case of |
| 7194 | // delayed device change |
| 7195 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 7196 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7197 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 7198 | } |
| 7199 | } |
| 7200 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7201 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 7202 | if (muteWaitMs > delayMs) { |
| 7203 | muteWaitMs -= delayMs; |
| 7204 | usleep(muteWaitMs * 1000); |
| 7205 | return muteWaitMs; |
| 7206 | } |
| 7207 | return 0; |
| 7208 | } |
| 7209 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7210 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 7211 | const DeviceVector &devices, |
| 7212 | bool force, |
| 7213 | int delayMs, |
| 7214 | audio_patch_handle_t *patchHandle, |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7215 | bool requiresMuteCheck, bool requiresVolumeCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7216 | { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 7217 | // TODO(b/262404095): Consider if the output need to be reopened. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7218 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7219 | uint32_t muteWaitMs; |
| 7220 | |
| 7221 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7222 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 7223 | nullptr /* patchHandle */, requiresMuteCheck); |
| 7224 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 7225 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7226 | return muteWaitMs; |
| 7227 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7228 | |
| 7229 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7230 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7231 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7232 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7233 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7234 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 7235 | |
| 7236 | if (!filteredDevices.isEmpty()) { |
| 7237 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7238 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7239 | |
| 7240 | // if the outputs are not materially active, there is no need to mute. |
| 7241 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7242 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 7243 | } else { |
| 7244 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 7245 | muteWaitMs = 0; |
| 7246 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7247 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7248 | bool outputRouted = outputDesc->isRouted(); |
| 7249 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7250 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 7251 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 7252 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7253 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 7254 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 7255 | // restore previous device after evaluating strategy mute state |
| 7256 | outputDesc->setDevices(prevDevices); |
| 7257 | return muteWaitMs; |
| 7258 | } |
| 7259 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7260 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 7261 | // the requested device is AUDIO_DEVICE_NONE |
| 7262 | // OR the requested device is the same as current device |
| 7263 | // AND force is not specified |
| 7264 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7265 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7266 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7267 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 7268 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7269 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
| 7270 | ALOGV("%s setting same device on routed output, force apply volumes", __func__); |
| 7271 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 7272 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7273 | return muteWaitMs; |
| 7274 | } |
| 7275 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7276 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7277 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7278 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 7279 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7280 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7281 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7282 | PatchBuilder patchBuilder; |
| 7283 | patchBuilder.addSource(outputDesc); |
| 7284 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 7285 | for (const auto &filteredDevice : filteredDevices) { |
| 7286 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 7287 | } |
| 7288 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 7289 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 7290 | // to avoid disordered sequence of muting volume and changing devices. |
| 7291 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), |
| 7292 | muteWaitMs == 0 ? (delayMs + (outputDesc->latency() / 2)) : delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7293 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7294 | |
| 7295 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7296 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7297 | |
| 7298 | return muteWaitMs; |
| 7299 | } |
| 7300 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7301 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7302 | int delayMs, |
| 7303 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7304 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7305 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 7306 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 7307 | return INVALID_OPERATION; |
| 7308 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7309 | if (patchHandle) { |
| 7310 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7311 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 7312 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7313 | } |
| 7314 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7315 | return INVALID_OPERATION; |
| 7316 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7317 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7318 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7319 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7320 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7321 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7322 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7323 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7324 | return status; |
| 7325 | } |
| 7326 | |
| 7327 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7328 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7329 | bool force, |
| 7330 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7331 | { |
| 7332 | status_t status = NO_ERROR; |
| 7333 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7334 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7335 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 7336 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7337 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7338 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7339 | PatchBuilder patchBuilder; |
| 7340 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 7341 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 7342 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7343 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 7344 | auto result = usecase; |
| 7345 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 7346 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 7347 | } |
| 7348 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7349 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7350 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7351 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7352 | } |
| 7353 | } |
| 7354 | return status; |
| 7355 | } |
| 7356 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7357 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 7358 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7359 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7360 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7361 | ssize_t index; |
| 7362 | if (patchHandle) { |
| 7363 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7364 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7365 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7366 | } |
| 7367 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7368 | return INVALID_OPERATION; |
| 7369 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7370 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7371 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7372 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7373 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7374 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7375 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7376 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7377 | return status; |
| 7378 | } |
| 7379 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7380 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7381 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7382 | audio_format_t& format, |
| 7383 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7384 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7385 | { |
| 7386 | // Choose an input profile based on the requested capture parameters: select the first available |
| 7387 | // profile supporting all requested parameters. |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7388 | // 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] | 7389 | // |
| 7390 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 7391 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7392 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7393 | const audio_input_flags_t mustMatchFlag = AUDIO_INPUT_FLAG_MMAP_NOIRQ; |
| 7394 | const audio_input_flags_t oriFlags = flags; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7395 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7396 | for (;;) { |
| 7397 | sp<IOProfile> firstInexact = nullptr; |
| 7398 | uint32_t updatedSamplingRate = 0; |
| 7399 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 7400 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
| 7401 | for (const auto& hwModule : mHwModules) { |
| 7402 | for (const auto& profile : hwModule->getInputProfiles()) { |
| 7403 | // profile->log(); |
| 7404 | //updatedFormat = format; |
| 7405 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
| 7406 | &samplingRate /*updatedSamplingRate*/, |
| 7407 | format, |
| 7408 | &format, /*updatedFormat*/ |
| 7409 | channelMask, |
| 7410 | &channelMask /*updatedChannelMask*/, |
| 7411 | // FIXME ugly cast |
| 7412 | (audio_output_flags_t) flags, |
| 7413 | true /*exactMatchRequiredForInputFlags*/)) { |
| 7414 | return profile; |
| 7415 | } |
| 7416 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
| 7417 | samplingRate, |
| 7418 | &updatedSamplingRate, |
| 7419 | format, |
| 7420 | &updatedFormat, |
| 7421 | channelMask, |
| 7422 | &updatedChannelMask, |
| 7423 | // FIXME ugly cast |
| 7424 | (audio_output_flags_t) flags, |
| 7425 | false /*exactMatchRequiredForInputFlags*/)) { |
| 7426 | firstInexact = profile; |
| 7427 | } |
| 7428 | } |
| 7429 | } |
| 7430 | |
| 7431 | if (firstInexact != nullptr) { |
| 7432 | samplingRate = updatedSamplingRate; |
| 7433 | format = updatedFormat; |
| 7434 | channelMask = updatedChannelMask; |
| 7435 | return firstInexact; |
| 7436 | } else if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 7437 | flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_RAW); // retry |
| 7438 | } else if ((flags & mustMatchFlag) == AUDIO_INPUT_FLAG_NONE && |
| 7439 | flags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(format)) { |
| 7440 | flags = AUDIO_INPUT_FLAG_NONE; |
| 7441 | } else { // fail |
| 7442 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 7443 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 7444 | samplingRate, format, channelMask, oriFlags); |
| 7445 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7446 | } |
| 7447 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7448 | |
| 7449 | return nullptr; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7450 | } |
| 7451 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7452 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 7453 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 7454 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7455 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7456 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7457 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7458 | |
| 7459 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 7460 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 7461 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 7462 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7463 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7464 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 7465 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 7466 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 7467 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7468 | |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7469 | if (volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7470 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 7471 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 7472 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7473 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7474 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7475 | } |
| 7476 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 7477 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7478 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 7479 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7480 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7481 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 7482 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7483 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 7484 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 7485 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7486 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7487 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7488 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7489 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7490 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 7491 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7492 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 7493 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 7494 | // programmatically muted. |
| 7495 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 7496 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 7497 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7498 | bool exemptFromCapping = |
| 7499 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 7500 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7501 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 7502 | volumeSource, volumeDb); |
| 7503 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7504 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 7505 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 7506 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 7507 | } |
| 7508 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7509 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 7510 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 7511 | // - always attenuate notifications volume by 6dB |
| 7512 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 7513 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7514 | // - if music is playing, always limit the volume to current music volume, |
| 7515 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7516 | if (!Intersection(deviceTypes, |
| 7517 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 7518 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 7519 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 7520 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7521 | ((volumeSource == alarmVolumeSrc || |
| 7522 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7523 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 7524 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 7525 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7526 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 7527 | curves.canBeMuted()) { |
| 7528 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7529 | // when the phone is ringing we must consider that music could have been paused just before |
| 7530 | // by the music application and behave as if music was active if the last music track was |
| 7531 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7532 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7533 | mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7534 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7535 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7536 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 7537 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7538 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7539 | float musicVolDb = computeVolume(musicCurves, |
| 7540 | musicVolumeSrc, |
| 7541 | musicCurves.getVolumeIndex(musicDevice), |
| 7542 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7543 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 7544 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 7545 | if (volumeDb > minVolDb) { |
| 7546 | volumeDb = minVolDb; |
| 7547 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7548 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 7549 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 7550 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 7551 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7552 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 7553 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7554 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7555 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7556 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 7557 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7558 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 7559 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7560 | } |
| 7561 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7562 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7563 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7564 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7565 | } |
| 7566 | } |
| 7567 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7568 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7569 | } |
| 7570 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7571 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7572 | VolumeSource fromVolumeSource, |
| 7573 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7574 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7575 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7576 | return srcIndex; |
| 7577 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7578 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 7579 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7580 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 7581 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 7582 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 7583 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7584 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 7585 | // preserve mute request or correct range |
| 7586 | if (srcIndex < minSrc) { |
| 7587 | if (srcIndex == 0) { |
| 7588 | return 0; |
| 7589 | } |
| 7590 | srcIndex = minSrc; |
| 7591 | } else if (srcIndex > maxSrc) { |
| 7592 | srcIndex = maxSrc; |
| 7593 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7594 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 7595 | } |
| 7596 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7597 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 7598 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7599 | int index, |
| 7600 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7601 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7602 | int delayMs, |
| 7603 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7604 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7605 | // do not change actual attributes volume if the attributes is muted |
| 7606 | if (outputDesc->isMuted(volumeSource)) { |
| 7607 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 7608 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7609 | return NO_ERROR; |
| 7610 | } |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7611 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7612 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
| 7613 | bool isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 7614 | bool isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7615 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7616 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7617 | bool isHAUsed = isHearingAidUsedForComm(); |
| 7618 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7619 | // 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] | 7620 | // if sco and call follow same curves, bypass forceUseForComm |
| 7621 | if ((callVolSrc != btScoVolSrc) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7622 | ((isVoiceVolSrc && isScoRequested) || |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7623 | (isBtScoVolSrc && !(isScoRequested || isHAUsed)))) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7624 | 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] | 7625 | volumeSource, isScoRequested ? " " : " not "); |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7626 | // Do not return an error here as AudioService will always set both voice call |
| 7627 | // and bluetooth SCO volumes due to stream aliasing. |
| 7628 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7629 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7630 | if (deviceTypes.empty()) { |
| 7631 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7632 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7633 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 7634 | if (curves.getVolumeIndexMin() < 0 || curves.getVolumeIndexMax() < 0) { |
| 7635 | ALOGE("invalid volume index range"); |
| 7636 | return BAD_VALUE; |
| 7637 | } |
| 7638 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7639 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 7640 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 7641 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 7642 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7643 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 7644 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7645 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7646 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7647 | outputDesc->setVolume( |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7648 | volumeDb, muted, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7649 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 7650 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7651 | float voiceVolume; |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7652 | // 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] | 7653 | if (isVoiceVolSrc) { |
| 7654 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7655 | } else { |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7656 | voiceVolume = index == 0 ? 0.0 : 1.0; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7657 | } |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 7658 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7659 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 7660 | mLastVoiceVolume = voiceVolume; |
| 7661 | } |
| 7662 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7663 | return NO_ERROR; |
| 7664 | } |
| 7665 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7666 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7667 | const DeviceTypeSet& deviceTypes, |
| 7668 | int delayMs, |
| 7669 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7670 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 7671 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7672 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 7673 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 7674 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7675 | curves.getVolumeIndex(deviceTypes), |
| 7676 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7677 | } |
| 7678 | } |
| 7679 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7680 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 7681 | bool on, |
| 7682 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7683 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7684 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7685 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7686 | std::vector<VolumeSource> sourcesToMute; |
| 7687 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 7688 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 7689 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7690 | VolumeSource source = toVolumeSource(attributes, false); |
| 7691 | if ((source != VOLUME_SOURCE_NONE) && |
| 7692 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 7693 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7694 | sourcesToMute.push_back(source); |
| 7695 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7696 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7697 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7698 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7699 | } |
| 7700 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7701 | } |
| 7702 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7703 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 7704 | bool on, |
| 7705 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7706 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7707 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7708 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7709 | if (deviceTypes.empty()) { |
| 7710 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7711 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7712 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7713 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7714 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7715 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7716 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7717 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 7718 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7719 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7720 | } |
| 7721 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7722 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 7723 | // ignored |
| 7724 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7725 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7726 | if (!outputDesc->isMuted(volumeSource)) { |
| 7727 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7728 | return; |
| 7729 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7730 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 7731 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7732 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7733 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7734 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7735 | delayMs); |
| 7736 | } |
| 7737 | } |
| 7738 | } |
| 7739 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7740 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 7741 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7742 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7743 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 7744 | return true; |
| 7745 | } |
| 7746 | |
| 7747 | // has known usage? |
| 7748 | switch (paa->usage) { |
| 7749 | case AUDIO_USAGE_UNKNOWN: |
| 7750 | case AUDIO_USAGE_MEDIA: |
| 7751 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7752 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7753 | case AUDIO_USAGE_ALARM: |
| 7754 | case AUDIO_USAGE_NOTIFICATION: |
| 7755 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7756 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7757 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7758 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7759 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7760 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7761 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7762 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7763 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7764 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 7765 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 7766 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 7767 | case AUDIO_USAGE_EMERGENCY: |
| 7768 | case AUDIO_USAGE_SAFETY: |
| 7769 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 7770 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7771 | break; |
| 7772 | default: |
| 7773 | return false; |
| 7774 | } |
| 7775 | return true; |
| 7776 | } |
| 7777 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7778 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 7779 | { |
| 7780 | return mEngine->getForceUse(usage); |
| 7781 | } |
| 7782 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7783 | bool AudioPolicyManager::isInCall() const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7784 | return isStateInCall(mEngine->getPhoneState()); |
| 7785 | } |
| 7786 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7787 | bool AudioPolicyManager::isStateInCall(int state) const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7788 | return is_state_in_call(state); |
| 7789 | } |
| 7790 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7791 | bool AudioPolicyManager::isCallAudioAccessible() const { |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7792 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7793 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7794 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 7795 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7796 | } |
| 7797 | |
Eric Laurent | f9cccec | 2022-11-16 19:12:00 +0100 | [diff] [blame] | 7798 | bool AudioPolicyManager::isInCallOrScreening() const { |
| 7799 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7800 | return isStateInCall(mode) || mode == AUDIO_MODE_CALL_SCREEN; |
| 7801 | } |
| 7802 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7803 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 7804 | { |
| 7805 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 7806 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7807 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 7808 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 7809 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7810 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7811 | } |
| 7812 | } |
| 7813 | |
| 7814 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 7815 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 7816 | bool release = false; |
| 7817 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 7818 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 7819 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 7820 | source->ext.device.type == deviceDesc->type()) { |
| 7821 | release = true; |
| 7822 | } |
| 7823 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7824 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7825 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 7826 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 7827 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7828 | sink->ext.device.type == deviceDesc->type() && |
| 7829 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 7830 | || strncmp(sink->ext.device.address, address, |
| 7831 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7832 | release = true; |
| 7833 | } |
| 7834 | } |
| 7835 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7836 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 7837 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7838 | } |
| 7839 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7840 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7841 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 7842 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7843 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7844 | } |
| 7845 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7846 | void AudioPolicyManager::modifySurroundFormats( |
| 7847 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7848 | std::unordered_set<audio_format_t> enforcedSurround( |
| 7849 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7850 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 7851 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 7852 | allSurround.insert(pair.first); |
| 7853 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 7854 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7855 | |
| 7856 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7857 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7858 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7859 | // This is the resulting set of formats depending on the surround mode: |
| 7860 | // 'all surround' = allSurround |
| 7861 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 7862 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 7863 | // 'manual surround' = mManualSurroundFormats |
| 7864 | // AUTO: formats v 'enforced surround' |
| 7865 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 7866 | // NEVER: formats ^ 'non-surround' |
| 7867 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7868 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7869 | std::unordered_set<audio_format_t> formatSet; |
| 7870 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 7871 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7872 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7873 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7874 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7875 | formatSet.insert(*formatIter); |
| 7876 | } |
| 7877 | } |
| 7878 | } else { |
| 7879 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 7880 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7881 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7882 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7883 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7884 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7885 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 7886 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 7887 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7888 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7889 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 7890 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 7891 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 7892 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7893 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7894 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7895 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7896 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7897 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7898 | } |
| 7899 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7900 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 7901 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7902 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7903 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 7904 | |
| 7905 | // If NEVER, then remove support for channelMasks > stereo. |
| 7906 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7907 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 7908 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7909 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 7910 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7911 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7912 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7913 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7914 | } |
| 7915 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7916 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 7917 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 7918 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7919 | bool supports5dot1 = false; |
| 7920 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 7921 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7922 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 7923 | supports5dot1 = true; |
| 7924 | break; |
| 7925 | } |
| 7926 | } |
| 7927 | // If not then add 5.1 support. |
| 7928 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7929 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 7930 | 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] | 7931 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7932 | } |
| 7933 | } |
| 7934 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7935 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 7936 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7937 | AudioProfileVector &profiles) |
| 7938 | { |
| 7939 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7940 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7941 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7942 | // Format MUST be checked first to update the list of AudioProfile |
| 7943 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7944 | reply = mpClientInterface->getParameters( |
| 7945 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7946 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7947 | AudioParameter repliedParameters(reply); |
| 7948 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7949 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7950 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 7951 | return; |
| 7952 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7953 | FormatVector formats = formatsFromString(reply.string()); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 7954 | mReportedFormatsMap[devDesc] = formats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7955 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 7956 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7957 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 7958 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 7959 | addProfilesForFormats(profiles, formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7960 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7961 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 7962 | for (audio_format_t format : profiles.getSupportedFormats()) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7963 | ChannelMaskSet channelMasks; |
| 7964 | SampleRateSet samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7965 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7966 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7967 | |
| 7968 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7969 | reply = mpClientInterface->getParameters( |
| 7970 | ioHandle, |
| 7971 | requestedParameters.toString() + ";" + |
| 7972 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7973 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7974 | AudioParameter repliedParameters(reply); |
| 7975 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7976 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7977 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7978 | } |
| 7979 | } |
| 7980 | if (profiles.hasDynamicChannelsFor(format)) { |
| 7981 | reply = mpClientInterface->getParameters(ioHandle, |
| 7982 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7983 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7984 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7985 | AudioParameter repliedParameters(reply); |
| 7986 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7987 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7988 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7989 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 7990 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7991 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7992 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7993 | } |
| 7994 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 7995 | addDynamicAudioProfileAndSort( |
| 7996 | profiles, new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7997 | } |
| 7998 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7999 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8000 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8001 | audio_patch_handle_t *patchHandle, |
| 8002 | AudioIODescriptorInterface *ioDescriptor, |
| 8003 | const struct audio_patch *patch, |
| 8004 | int delayMs) |
| 8005 | { |
| 8006 | ssize_t index = mAudioPatches.indexOfKey( |
| 8007 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 8008 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 8009 | sp<AudioPatch> patchDesc; |
| 8010 | status_t status = installPatch( |
| 8011 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 8012 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8013 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8014 | } |
| 8015 | return status; |
| 8016 | } |
| 8017 | |
| 8018 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 8019 | ssize_t index, |
| 8020 | audio_patch_handle_t *patchHandle, |
| 8021 | const struct audio_patch *patch, |
| 8022 | int delayMs, |
| 8023 | uid_t uid, |
| 8024 | sp<AudioPatch> *patchDescPtr) |
| 8025 | { |
| 8026 | sp<AudioPatch> patchDesc; |
| 8027 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 8028 | if (index >= 0) { |
| 8029 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8030 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 8034 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 8035 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 8036 | if (status == NO_ERROR) { |
| 8037 | if (index < 0) { |
| 8038 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8039 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8040 | } else { |
| 8041 | patchDesc->mPatch = *patch; |
| 8042 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8043 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8044 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 8045 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 8046 | } |
| 8047 | nextAudioPortGeneration(); |
| 8048 | mpClientInterface->onAudioPatchListUpdate(); |
| 8049 | } |
| 8050 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 8051 | return status; |
| 8052 | } |
| 8053 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8054 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 8055 | { |
| 8056 | const TrackClientVector activeClients = output->getActiveClients(); |
| 8057 | if (activeClients.empty()) { |
| 8058 | return true; |
| 8059 | } |
| 8060 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 8061 | if (index < 0) { |
| 8062 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 8063 | __func__, output->getId()); |
| 8064 | return false; |
| 8065 | } |
| 8066 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 8067 | DeviceVector routedDevices; |
| 8068 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 8069 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 8070 | patchDesc->mPatch.sinks[i].id); |
| 8071 | if (device == nullptr) { |
| 8072 | ALOGE("%s, no audio device found with id(%d)", |
| 8073 | __func__, patchDesc->mPatch.sinks[i].id); |
| 8074 | return false; |
| 8075 | } |
| 8076 | routedDevices.add(device); |
| 8077 | } |
| 8078 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 8079 | if (client->isInvalid()) { |
| 8080 | // No need to take care about invalidated clients. |
| 8081 | continue; |
| 8082 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8083 | sp<DeviceDescriptor> preferredDevice = |
| 8084 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 8085 | if (mEngine->getOutputDevicesForAttributes( |
| 8086 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 8087 | return false; |
| 8088 | } |
| 8089 | } |
| 8090 | return true; |
| 8091 | } |
| 8092 | |
| 8093 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8094 | const sp<IOProfile>& profile, const DeviceVector& devices, |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8095 | const audio_config_base_t *mixerConfig, const audio_config_t *halConfig, |
| 8096 | audio_output_flags_t flags) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8097 | { |
| 8098 | for (const auto& device : devices) { |
| 8099 | // TODO: This should be checking if the profile supports the device combo. |
| 8100 | if (!profile->supportsDevice(device)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8101 | ALOGE("%s profile(%s) doesn't support device %#x", __func__, profile->getName().c_str(), |
| 8102 | device->type()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8103 | return nullptr; |
| 8104 | } |
| 8105 | } |
| 8106 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 8107 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8108 | status_t status = desc->open(halConfig, mixerConfig, devices, |
| 8109 | AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8110 | if (status != NO_ERROR) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8111 | ALOGE("%s failed to open output %d", __func__, status); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8112 | return nullptr; |
| 8113 | } |
| 8114 | |
| 8115 | // Here is where the out_set_parameters() for card & device gets called |
| 8116 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 8117 | const audio_devices_t deviceType = device->type(); |
| 8118 | const String8 &address = String8(device->address().c_str()); |
| 8119 | if (!address.isEmpty()) { |
| 8120 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 8121 | mpClientInterface->setParameters(output, String8(param)); |
| 8122 | free(param); |
| 8123 | } |
| 8124 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
| 8125 | if (!profile->hasValidAudioProfile()) { |
| 8126 | ALOGW("%s() missing param", __func__); |
| 8127 | desc->close(); |
| 8128 | return nullptr; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8129 | } else if (profile->hasDynamicAudioProfile() && halConfig == nullptr) { |
| 8130 | // Reopen the output with the best audio profile picked by APM when the profile supports |
| 8131 | // dynamic audio profile and the hal config is not specified. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8132 | desc->close(); |
| 8133 | output = AUDIO_IO_HANDLE_NONE; |
| 8134 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 8135 | profile->pickAudioProfile( |
| 8136 | config.sample_rate, config.channel_mask, config.format); |
| 8137 | config.offload_info.sample_rate = config.sample_rate; |
| 8138 | config.offload_info.channel_mask = config.channel_mask; |
| 8139 | config.offload_info.format = config.format; |
| 8140 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8141 | status = desc->open(&config, mixerConfig, devices, AUDIO_STREAM_DEFAULT, flags, &output); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8142 | if (status != NO_ERROR) { |
| 8143 | return nullptr; |
| 8144 | } |
| 8145 | } |
| 8146 | |
| 8147 | addOutput(output, desc); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8148 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8149 | sp<DeviceDescriptor> speaker = mAvailableOutputDevices.getDevice( |
| 8150 | AUDIO_DEVICE_OUT_SPEAKER, String8(""), AUDIO_FORMAT_DEFAULT); |
| 8151 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8152 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 8153 | sp<AudioPolicyMix> policyMix; |
| 8154 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 8155 | policyMix->setOutput(desc); |
| 8156 | desc->mPolicyMix = policyMix; |
| 8157 | } else { |
| 8158 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
| 8159 | address.string()); |
| 8160 | } |
| 8161 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8162 | } else if (hasPrimaryOutput() && speaker != nullptr |
| 8163 | && mPrimaryOutput->supportsDevice(speaker) && !desc->supportsDevice(speaker) |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 8164 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 8165 | // no duplicated output for: |
| 8166 | // - direct outputs |
| 8167 | // - outputs used by dynamic policy mixes |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 8168 | // - outputs that supports SPEAKER while the primary output does not. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8169 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 8170 | |
| 8171 | //TODO: configure audio effect output stage here |
| 8172 | |
| 8173 | // open a duplicating output thread for the new output and the primary output |
| 8174 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 8175 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 8176 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 8177 | if (status == NO_ERROR) { |
| 8178 | // add duplicated output descriptor |
| 8179 | addOutput(duplicatedOutput, dupOutputDesc); |
| 8180 | } else { |
| 8181 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 8182 | mPrimaryOutput->mIoHandle, output); |
| 8183 | desc->close(); |
| 8184 | removeOutput(output); |
| 8185 | nextAudioPortGeneration(); |
| 8186 | return nullptr; |
| 8187 | } |
| 8188 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 8189 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 8190 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 8191 | mPrimaryOutput = desc; |
| 8192 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 8193 | return desc; |
| 8194 | } |
| 8195 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8196 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 8197 | const audio_attributes_t &attr, DeviceVector &devices, bool forVolume) { |
| 8198 | // Devices are determined in the following precedence: |
| 8199 | // |
| 8200 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 8201 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 8202 | // |
| 8203 | // If no such dynamic policy then |
| 8204 | // 2) Devices containing an active client using setPreferredDevice |
| 8205 | // with same strategy as the attributes. |
| 8206 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8207 | // |
| 8208 | // If no corresponding active client with setPreferredDevice then |
| 8209 | // 3) Devices associated with the strategy determined by the attributes |
| 8210 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 8211 | // |
| 8212 | // See related getOutputForAttrInt(). |
| 8213 | |
| 8214 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 8215 | // audio routing, only used for duplication for playback capture) |
| 8216 | sp<AudioPolicyMix> policyMix; |
| 8217 | status_t status = mPolicyMixes.getOutputForAttr(attr, AUDIO_CONFIG_BASE_INITIALIZER, |
Jan Sebechlebsky | 1a80c06 | 2022-08-09 15:21:18 +0200 | [diff] [blame] | 8218 | 0 /*uid unknown here*/, AUDIO_SESSION_NONE, AUDIO_OUTPUT_FLAG_NONE, policyMix, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 8219 | nullptr /* secondaryMixes */); |
| 8220 | if (status != OK) { |
| 8221 | return status; |
| 8222 | } |
| 8223 | |
| 8224 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 8225 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 8226 | // as they are unaffected by device/stream volume |
| 8227 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 8228 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 8229 | ) { |
| 8230 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 8231 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 8232 | devices.add(deviceDesc); |
| 8233 | } else { |
| 8234 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 8235 | // which selects setPreferredDevice if active. This means forVolume call |
| 8236 | // will take an active setPreferredDevice, if such exists. |
| 8237 | |
| 8238 | devices = mEngine->getOutputDevicesForAttributes( |
| 8239 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
| 8240 | } |
| 8241 | |
| 8242 | if (forVolume) { |
| 8243 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 8244 | // for single volume control in AudioService (such relationship should exist if |
| 8245 | // SPEAKER_SAFE is present). |
| 8246 | // |
| 8247 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 8248 | DeviceVector speakerSafeDevices = |
| 8249 | devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 8250 | if (!speakerSafeDevices.isEmpty()) { |
| 8251 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 8252 | devices.remove(speakerSafeDevices); |
| 8253 | } |
| 8254 | } |
| 8255 | |
| 8256 | return NO_ERROR; |
| 8257 | } |
| 8258 | |
| 8259 | status_t AudioPolicyManager::getProfilesForDevices(const DeviceVector& devices, |
| 8260 | AudioProfileVector& audioProfiles, |
| 8261 | uint32_t flags, |
| 8262 | bool isInput) { |
| 8263 | for (const auto& hwModule : mHwModules) { |
| 8264 | // the MSD module checks for different conditions |
| 8265 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 8266 | continue; |
| 8267 | } |
| 8268 | IOProfileCollection ioProfiles = isInput ? hwModule->getInputProfiles() |
| 8269 | : hwModule->getOutputProfiles(); |
| 8270 | for (const auto& profile : ioProfiles) { |
| 8271 | if (!profile->areAllDevicesSupported(devices) || |
| 8272 | !profile->isCompatibleProfileForFlags( |
| 8273 | flags, false /*exactMatchRequiredForInputFlags*/)) { |
| 8274 | continue; |
| 8275 | } |
| 8276 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8277 | } |
| 8278 | } |
| 8279 | |
| 8280 | if (!isInput) { |
| 8281 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 8282 | const auto &msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 8283 | if (msdModule != nullptr) { |
| 8284 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 8285 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 8286 | for (const auto &profile: msdModule->getOutputProfiles()) { |
| 8287 | if (!profile->asAudioPort()->isDirectOutput()) { |
| 8288 | continue; |
| 8289 | } |
| 8290 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 8291 | } |
| 8292 | } else { |
| 8293 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 8294 | } |
| 8295 | } |
| 8296 | } |
| 8297 | |
| 8298 | return NO_ERROR; |
| 8299 | } |
| 8300 | |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8301 | sp<SwAudioOutputDescriptor> AudioPolicyManager::reopenOutput(sp<SwAudioOutputDescriptor> outputDesc, |
| 8302 | const audio_config_t *config, |
| 8303 | audio_output_flags_t flags, |
| 8304 | const char* caller) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8305 | closeOutput(outputDesc->mIoHandle); |
| 8306 | sp<SwAudioOutputDescriptor> preferredOutput = openOutputWithProfileAndDevice( |
| 8307 | outputDesc->mProfile, outputDesc->devices(), nullptr /*mixerConfig*/, config, flags); |
| 8308 | if (preferredOutput == nullptr) { |
| 8309 | ALOGE("%s failed to reopen output device=%d, caller=%s", |
| 8310 | __func__, outputDesc->devices()[0]->getId(), caller); |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8311 | } |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 8312 | return preferredOutput; |
| 8313 | } |
| 8314 | |
| 8315 | void AudioPolicyManager::reopenOutputsWithDevices( |
| 8316 | const std::map<audio_io_handle_t, DeviceVector> &outputsToReopen) { |
| 8317 | for (const auto& [output, devices] : outputsToReopen) { |
| 8318 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 8319 | closeOutput(output); |
| 8320 | openOutputWithProfileAndDevice(desc->mProfile, devices); |
| 8321 | } |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 8322 | } |
| 8323 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 8324 | } // namespace android |