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 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | } break; |
| 264 | |
| 265 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 266 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 267 | return BAD_VALUE; |
| 268 | } |
| 269 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 270 | // Propagate device availability to Engine |
| 271 | setEngineDeviceConnectionState(device, state); |
| 272 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 273 | // No need to evaluate playback routing when connecting a remote submix |
| 274 | // output device used by a dynamic policy of type recorder as no |
| 275 | // playback use case is affected. |
| 276 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 277 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 278 | for (audio_io_handle_t output : outputs) { |
| 279 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 280 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 281 | if (policyMix != nullptr |
| 282 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 283 | && device->address() == policyMix->mDeviceAddress.string()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 284 | doCheckForDeviceAndOutputChanges = false; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 291 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 292 | if (!outputs.isEmpty()) { |
| 293 | for (audio_io_handle_t output : outputs) { |
| 294 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 295 | // close unused outputs after device disconnection or direct outputs that have |
| 296 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 297 | // "outputs" vector never contains duplicated outputs |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 298 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 299 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 300 | (desc->mDirectOpenCount == 0)) |
| 301 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) && |
| 302 | !isOutputOnlyAvailableRouteToSomeDevice(desc))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 303 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 304 | closeOutput(output); |
| 305 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 306 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 307 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 308 | return true; |
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 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 311 | }; |
| 312 | |
| 313 | if (doCheckForDeviceAndOutputChanges) { |
| 314 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 315 | } else { |
| 316 | checkCloseOutputs(); |
| 317 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 318 | (void)updateCallRouting(false /*fromCache*/); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 319 | std::vector<audio_io_handle_t> outputsToReopen; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 320 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 321 | const DeviceVector activeMediaDevices = |
| 322 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 323 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 324 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 325 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 326 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 327 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 328 | // do not force device change on duplicated output because if device is 0, it will |
| 329 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 330 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 331 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 332 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 333 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 334 | // always force when disconnecting (a non-duplicated device) |
| 335 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 336 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 337 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 338 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 339 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 340 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 341 | // Reopen the output to query the dynamic profiles when there is not active |
| 342 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 343 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 344 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 345 | if (areAllActiveTracksRerouted(desc)) { |
| 346 | outputsToReopen.push_back(mOutputs.keyAt(i)); |
| 347 | } else { |
| 348 | desc->mPendingReopenToQueryProfiles = true; |
| 349 | } |
| 350 | } |
| 351 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 352 | // Clear the flag that previously set for re-querying profiles. |
| 353 | desc->mPendingReopenToQueryProfiles = false; |
| 354 | } |
| 355 | } |
| 356 | for (const auto& output : outputsToReopen) { |
| 357 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 358 | closeOutput(output); |
| 359 | openOutputWithProfileAndDevice(desc->mProfile, activeMediaDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 362 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 363 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 366 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, 0); |
| 367 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 368 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 369 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 370 | } // end if is output device |
| 371 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 372 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 373 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 374 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 375 | switch (state) |
| 376 | { |
| 377 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 378 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 379 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 380 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 381 | return INVALID_OPERATION; |
| 382 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 383 | |
| 384 | if (mAvailableInputDevices.add(device) < 0) { |
| 385 | return NO_MEMORY; |
| 386 | } |
| 387 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 388 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 389 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 390 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 391 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 392 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 393 | mAvailableInputDevices.remove(device); |
| 394 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 395 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 396 | |
| 397 | mHwModules.cleanUpForDevice(device); |
| 398 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 399 | return INVALID_OPERATION; |
| 400 | } |
| 401 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 402 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 403 | |
| 404 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 405 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 406 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 407 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 408 | return INVALID_OPERATION; |
| 409 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 410 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 411 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 412 | |
| 413 | // Set Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 414 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 415 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 416 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 417 | |
| 418 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 419 | |
| 420 | // remove device from mReportedFormatsMap cache |
| 421 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 422 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 423 | |
| 424 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 425 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 426 | return BAD_VALUE; |
| 427 | } |
| 428 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 429 | // Propagate device availability to Engine |
| 430 | setEngineDeviceConnectionState(device, state); |
| 431 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 432 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 433 | // As the input device list can impact the output device selection, update |
| 434 | // getDeviceForStrategy() cache |
| 435 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 436 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 437 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 438 | // Reconnect Audio Source |
| 439 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 440 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 441 | checkAudioSourceForAttributes(attributes); |
| 442 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 443 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 444 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 447 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 448 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 449 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 450 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 451 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 452 | return BAD_VALUE; |
| 453 | } |
| 454 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 455 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 456 | const char* device_name, |
| 457 | media::AudioPort* aidlPort) { |
| 458 | DeviceDescriptorBase devDescr(device, device_address); |
| 459 | devDescr.setName(device_name); |
| 460 | return devDescr.writeToParcelable(aidlPort); |
| 461 | } |
| 462 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 463 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 464 | audio_policy_dev_state_t state) { |
| 465 | |
| 466 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 467 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 468 | return; |
| 469 | } |
| 470 | mEngine->setDeviceConnectionState(device, state); |
| 471 | } |
| 472 | |
| 473 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 474 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 475 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 476 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 477 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 478 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 479 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 480 | (strlen(device_address) != 0)/*matchAddress*/); |
| 481 | |
| 482 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 483 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 484 | device, device_address); |
| 485 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 486 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 487 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 488 | DeviceVector *deviceVector; |
| 489 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 490 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 491 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 492 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 493 | deviceVector = &mAvailableInputDevices; |
| 494 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 495 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 496 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 497 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 498 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 499 | return (deviceVector->getDevice( |
| 500 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 501 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 504 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 505 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 506 | const char *device_name, |
| 507 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 508 | { |
| 509 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 510 | String8 reply; |
| 511 | AudioParameter param; |
| 512 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 513 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 514 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 515 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 516 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 517 | // connect/disconnect only 1 device at a time |
| 518 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 519 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 520 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 521 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 522 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 523 | // Nothing to do: device is not connected |
| 524 | return NO_ERROR; |
| 525 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 526 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 527 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 528 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 529 | // configure codecs. |
| 530 | // Handle two specific cases by sending a set parameter to |
| 531 | // configure A2DP codecs. No need to toggle device state. |
| 532 | // Case 1: A2DP active device switches from primary to primary |
| 533 | // module |
| 534 | // Case 2: A2DP device config changes on primary module. |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 535 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 536 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 537 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 538 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 539 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 540 | reply = mpClientInterface->getParameters( |
| 541 | AUDIO_IO_HANDLE_NONE, |
| 542 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 543 | AudioParameter repliedParameters(reply); |
| 544 | repliedParameters.getInt( |
| 545 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 546 | if (isReconfigA2dpSupported) { |
| 547 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 548 | param.add(key, String8("true")); |
| 549 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 550 | devDesc->setEncodedFormat(encodedFormat); |
| 551 | return NO_ERROR; |
| 552 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 555 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 556 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 557 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 558 | // mute media strategies and delay device switch by the largest |
| 559 | // This avoid sending the music tail into the earpiece or headset. |
| 560 | setStrategyMute(musicStrategy, true, desc); |
| 561 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 562 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 563 | nullptr, true /*fromCache*/).types()); |
| 564 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 565 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 566 | // This will force reading again the device configuration |
| 567 | status = setDeviceConnectionState(device, |
| 568 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 569 | device_address, device_name, |
| 570 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 571 | if (status != NO_ERROR) { |
| 572 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 573 | status); |
| 574 | return status; |
| 575 | } |
| 576 | |
| 577 | status = setDeviceConnectionState(device, |
| 578 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 579 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 580 | if (status != NO_ERROR) { |
| 581 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 582 | status); |
| 583 | return status; |
| 584 | } |
| 585 | |
| 586 | return NO_ERROR; |
| 587 | } |
| 588 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 589 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 590 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 591 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 592 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 593 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 594 | std::unordered_set<audio_format_t> formatSet; |
| 595 | sp<HwModule> primaryModule = |
| 596 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 597 | if (primaryModule == nullptr) { |
| 598 | ALOGE("%s() unable to get primary module", __func__); |
| 599 | return NO_INIT; |
| 600 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 601 | |
| 602 | DeviceTypeSet audioDeviceSet; |
| 603 | |
| 604 | switch(device) { |
| 605 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 606 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 607 | break; |
| 608 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
| 609 | audioDeviceSet = getAudioDeviceOutAllBleSet(); |
| 610 | break; |
| 611 | default: |
| 612 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 613 | return BAD_VALUE; |
| 614 | } |
| 615 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 616 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 617 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 618 | for (const auto& device : declaredDevices) { |
| 619 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 620 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 621 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 622 | return status; |
| 623 | } |
| 624 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 625 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 626 | { |
| 627 | DeviceVector rxSinkdevices{}; |
| 628 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 629 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 630 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 631 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 632 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 633 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 634 | // retrieve Rx Source device descriptor |
| 635 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 636 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 637 | |
| 638 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 639 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 640 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 641 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 642 | return DeviceVector(rxSinkDevice); |
| 643 | } |
| 644 | } |
| 645 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 646 | // the device returned is not necessarily reachable via this output |
| 647 | // (filter later by setOutputDevices()) |
| 648 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 649 | } |
| 650 | |
| 651 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 652 | { |
| 653 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
| 654 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 655 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 656 | } |
| 657 | return INVALID_OPERATION; |
| 658 | } |
| 659 | |
| 660 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 661 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 662 | { |
| 663 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 664 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 665 | uint32_t muteWaitMs = 0; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 666 | if(!hasPrimaryOutput() || |
| 667 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 668 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 669 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 670 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 671 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 672 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 673 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 674 | ALOG_ASSERT(txSourceDevice != 0, "%s() input selected device not available", __func__); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 675 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 676 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 677 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 678 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 679 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 680 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 681 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 682 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 683 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 684 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 685 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 686 | // retrieve Rx Source and Tx Sink device descriptors |
| 687 | sp<DeviceDescriptor> rxSourceDevice = |
| 688 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 689 | String8(), |
| 690 | AUDIO_FORMAT_DEFAULT); |
| 691 | sp<DeviceDescriptor> txSinkDevice = |
| 692 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 693 | String8(), |
| 694 | AUDIO_FORMAT_DEFAULT); |
| 695 | |
| 696 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 697 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 698 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 699 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 700 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 701 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 702 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 703 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 704 | // createAudioPatchInternal now supports both HW / SW bridging |
| 705 | createRxPatch = true; |
| 706 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 707 | } else { |
| 708 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 709 | // voice calls via setOutputDevice() on primary output. |
| 710 | // Otherwise, create two audio patches for TX and RX path. |
| 711 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 712 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 713 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 714 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 715 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 716 | (txSinkDevice != 0); |
| 717 | } |
| 718 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 719 | // Otherwise, create two audio patches for TX and RX path. |
| 720 | if (!createRxPatch) { |
| 721 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 722 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 723 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 724 | // If the TX device is on the primary HW module but RX device is |
| 725 | // on other HW module, SinkMetaData of telephony input should handle it |
| 726 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 727 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 728 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 729 | // terminate active capture if on the same HW module as the call TX source device |
| 730 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 731 | // call TX device but this information is not in the audio patch and logic here must be |
| 732 | // symmetric to the one in startInput() |
| 733 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 734 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 735 | closeActiveClients(activeDesc); |
| 736 | } |
| 737 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 738 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 739 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 740 | if (waitMs != nullptr) { |
| 741 | *waitMs = muteWaitMs; |
| 742 | } |
| 743 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 744 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 745 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 746 | bool AudioPolicyManager::isDeviceOfModule( |
| 747 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 748 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 749 | if (module != 0) { |
| 750 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 751 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 752 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 753 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 754 | } |
| 755 | return false; |
| 756 | } |
| 757 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 758 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 759 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 760 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 761 | const struct audio_port_config source = { |
| 762 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 763 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 764 | }; |
| 765 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 766 | mCallRxSourceClient = startAudioSourceInternal(&source, &aa, 0/*uid*/); |
| 767 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 768 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 771 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 772 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 773 | if (clientDesc == nullptr) { |
| 774 | return; |
| 775 | } |
| 776 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 777 | "%s error stopping audio source", __func__); |
| 778 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 782 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 783 | uint32_t delayMs) |
| 784 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 785 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 786 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 787 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 788 | return; |
| 789 | } |
| 790 | PatchBuilder patchBuilder; |
| 791 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
| 792 | ALOGV("%s between source %s and sink %s", __func__, |
| 793 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 794 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 795 | const audio_attributes_t aa = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
| 796 | struct audio_port_config source = {}; |
| 797 | srcDevice->toAudioPortConfig(&source); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 798 | mCallTxSourceClient = new InternalSourceClientDescriptor( |
| 799 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, sinkDevice, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 800 | mCommunnicationStrategy, toVolumeSource(aa)); |
| 801 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 802 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 803 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 804 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 805 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
| 806 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 807 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 808 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 811 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 812 | { |
| 813 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 814 | // store previous phone state for management of sonification strategy below |
| 815 | int oldState = mEngine->getPhoneState(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 816 | bool wasLeUnicastActive = isLeUnicastActive(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 817 | |
| 818 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 819 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 820 | return; |
| 821 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 822 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 823 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 824 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 825 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 826 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 827 | } |
| 828 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 829 | /** |
| 830 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 831 | * routing command. |
| 832 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 833 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 834 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 835 | |
| 836 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 837 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 838 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 839 | int delayMs = 0; |
| 840 | if (isStateInCall(state)) { |
| 841 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 842 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 843 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 844 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 845 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 846 | // mute media and sonification strategies and delay device switch by the largest |
| 847 | // latency of any output where either strategy is active. |
| 848 | // 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] | 849 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 850 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 851 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 852 | (delayMs < (int)desc->latency()*2)) { |
| 853 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 854 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 855 | setStrategyMute(musicStrategy, true, desc); |
| 856 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 857 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 858 | nullptr, true /*fromCache*/).types()); |
| 859 | setStrategyMute(sonificationStrategy, true, desc); |
| 860 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 861 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 862 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 866 | if (hasPrimaryOutput()) { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 867 | if (state == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 868 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 869 | } else { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 870 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 871 | // force routing command to audio hardware when ending call |
| 872 | // even if no device change is needed |
| 873 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 874 | rxDevices = mPrimaryOutput->devices(); |
| 875 | } |
| 876 | if (oldState == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 877 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 878 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 879 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 880 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 881 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 882 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 883 | |
| 884 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 885 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 886 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 887 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 888 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 889 | bool forceRouting = !newDevices.isEmpty(); |
| 890 | setOutputDevices(desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
| 891 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 892 | } |
| 893 | } |
| 894 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 895 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
| 896 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 897 | if (isStateInCall(state)) { |
| 898 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 899 | // force reevaluating accessibility routing when call starts |
| 900 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | // 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] | 904 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 905 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 908 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 909 | return mEngine->getPhoneState(); |
| 910 | } |
| 911 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 912 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 913 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 914 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 915 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 916 | if (config == mEngine->getForceUse(usage)) { |
| 917 | return; |
| 918 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 919 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 920 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 921 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 922 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 923 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 924 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 925 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 926 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 927 | |
| 928 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 929 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 930 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 931 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 932 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
| 933 | mpClientInterface->invalidateStream(AUDIO_STREAM_SYSTEM); |
| 934 | mpClientInterface->invalidateStream(AUDIO_STREAM_ENFORCED_AUDIBLE); |
| 935 | } |
| 936 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 937 | //FIXME: workaround for truncated touch sounds |
| 938 | // to be removed when the problem is handled by system UI |
| 939 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 940 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 941 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 942 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 943 | |
| 944 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 945 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 948 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 949 | { |
| 950 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 951 | } |
| 952 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 953 | // Find an MSD output profile compatible with the parameters passed. |
| 954 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 955 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 956 | const DeviceVector& devices, |
| 957 | uint32_t samplingRate, |
| 958 | audio_format_t format, |
| 959 | audio_channel_mask_t channelMask, |
| 960 | audio_output_flags_t flags, |
| 961 | bool directOnly) |
| 962 | { |
| 963 | flags = getRelevantFlags(flags, directOnly); |
| 964 | |
| 965 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 966 | if (msdModule != nullptr) { |
| 967 | // for the msd module check if there are patches to the output devices |
| 968 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 969 | HwModuleCollection modules; |
| 970 | modules.add(msdModule); |
| 971 | return searchCompatibleProfileHwModules( |
| 972 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 973 | flags, directOnly); |
| 974 | } |
| 975 | } |
| 976 | return nullptr; |
| 977 | } |
| 978 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 979 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 980 | // search to profiles for direct outputs. |
| 981 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 982 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 983 | uint32_t samplingRate, |
| 984 | audio_format_t format, |
| 985 | audio_channel_mask_t channelMask, |
| 986 | audio_output_flags_t flags, |
| 987 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 988 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 989 | flags = getRelevantFlags(flags, directOnly); |
| 990 | |
| 991 | return searchCompatibleProfileHwModules( |
| 992 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 993 | } |
| 994 | |
| 995 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 996 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 997 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 998 | // only retain flags that will drive the direct output profile selection |
| 999 | // if explicitly requested |
| 1000 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1001 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1002 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1003 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1004 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1005 | return flags; |
| 1006 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1007 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1008 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 1009 | const HwModuleCollection& hwModules, |
| 1010 | const DeviceVector& devices, |
| 1011 | uint32_t samplingRate, |
| 1012 | audio_format_t format, |
| 1013 | audio_channel_mask_t channelMask, |
| 1014 | audio_output_flags_t flags, |
| 1015 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1016 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1017 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1018 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1019 | if (!curProfile->isCompatibleProfile(devices, |
| 1020 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1021 | format, NULL /*updatedFormat*/, |
| 1022 | channelMask, NULL /*updatedChannelMask*/, |
| 1023 | flags)) { |
| 1024 | continue; |
| 1025 | } |
| 1026 | // reject profiles not corresponding to a device currently available |
| 1027 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1028 | continue; |
| 1029 | } |
| 1030 | // reject profiles if connected device does not support codec |
| 1031 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1032 | continue; |
| 1033 | } |
| 1034 | if (!directOnly) { |
| 1035 | return curProfile; |
| 1036 | } |
| 1037 | |
| 1038 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1039 | // to one with offload capability |
| 1040 | if (profile != 0 && |
| 1041 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1042 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1043 | } |
| 1044 | profile = curProfile; |
| 1045 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1046 | break; |
| 1047 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1048 | } |
| 1049 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1050 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1053 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1054 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1055 | { |
| 1056 | for (const auto& hwModule : mHwModules) { |
| 1057 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1058 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1059 | continue; |
| 1060 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1061 | if (!devices.empty()) { |
Eric Laurent | 0c8f7cc | 2022-06-24 14:32:36 +0200 | [diff] [blame] | 1062 | // reject profiles not corresponding to a device currently available |
| 1063 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1064 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1065 | continue; |
| 1066 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1067 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1068 | != devices.size()) { |
| 1069 | continue; |
| 1070 | } |
| 1071 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1072 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1073 | return curProfile; |
| 1074 | } |
| 1075 | } |
| 1076 | return nullptr; |
| 1077 | } |
| 1078 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1079 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1080 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1081 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1082 | |
| 1083 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1084 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1085 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1086 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1087 | // and AudioSystem::getOutputSamplingRate(). |
| 1088 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1089 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1090 | const audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1091 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1092 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1093 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1094 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1097 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1098 | const audio_attributes_t *srcAttr, |
| 1099 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1100 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1101 | if (srcAttr != NULL) { |
| 1102 | if (!isValidAttributes(srcAttr)) { |
| 1103 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1104 | __func__, |
| 1105 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1106 | srcAttr->tags); |
| 1107 | return BAD_VALUE; |
| 1108 | } |
| 1109 | *dstAttr = *srcAttr; |
| 1110 | } else { |
| 1111 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1112 | ALOGE("%s: invalid stream type", __func__); |
| 1113 | return BAD_VALUE; |
| 1114 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1115 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1116 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1117 | |
| 1118 | // Only honor audibility enforced when required. The client will be |
| 1119 | // forced to reconnect if the forced usage changes. |
| 1120 | 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] | 1121 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1122 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1125 | return NO_ERROR; |
| 1126 | } |
| 1127 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1128 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1129 | audio_attributes_t *resultAttr, |
| 1130 | audio_io_handle_t *output, |
| 1131 | audio_session_t session, |
| 1132 | const audio_attributes_t *attr, |
| 1133 | audio_stream_type_t *stream, |
| 1134 | uid_t uid, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1135 | audio_config_t *config, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1136 | audio_output_flags_t *flags, |
| 1137 | audio_port_handle_t *selectedDeviceId, |
| 1138 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1139 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1140 | output_type_t *outputType, |
| 1141 | bool *isSpatialized) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1142 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1143 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1144 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1145 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1146 | const sp<DeviceDescriptor> requestedDevice = |
| 1147 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1148 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1149 | *outputType = API_OUTPUT_INVALID; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1150 | *isSpatialized = false; |
| 1151 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1152 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1153 | if (status != NO_ERROR) { |
| 1154 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1155 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1156 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1157 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1158 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1159 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1160 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1161 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1162 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1163 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1164 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1165 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1166 | // 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] | 1167 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1168 | const audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1169 | .channel_mask = config->channel_mask, |
| 1170 | .format = config->format, |
| 1171 | }; |
| 1172 | status = mPolicyMixes.getOutputForAttr(*resultAttr, clientConfig, uid, *flags, primaryMix, |
| 1173 | secondaryMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1174 | if (status != OK) { |
| 1175 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1176 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1177 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1178 | // Explicit routing is higher priority then any dynamic policy primary output |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1179 | bool usePrimaryOutputFromPolicyMixes = requestedDevice == nullptr && primaryMix != nullptr; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1180 | |
| 1181 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 1182 | if ((secondaryMixes != nullptr && !secondaryMixes->empty()) |
| 1183 | && !audio_is_linear_pcm(config->format)) { |
| 1184 | ALOGD("%s: rejecting request as secondary mixes only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1185 | return BAD_VALUE; |
| 1186 | } |
| 1187 | if (usePrimaryOutputFromPolicyMixes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1188 | sp<DeviceDescriptor> deviceDesc = |
| 1189 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1190 | primaryMix->mDeviceAddress, |
| 1191 | AUDIO_FORMAT_DEFAULT); |
| 1192 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1193 | bool tryDirectForFlags = policyDesc == nullptr || |
| 1194 | (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT); |
| 1195 | // if a direct output can be opened to deliver the track's multi-channel content to the |
| 1196 | // output rather than being downmixed by the primary output, then use this direct |
| 1197 | // output by by-passing the primary mix if possible, otherwise fall-through to primary |
| 1198 | // mix. |
| 1199 | bool tryDirectForChannelMask = policyDesc != nullptr |
| 1200 | && (audio_channel_count_from_out_mask(policyDesc->getConfig().channel_mask) < |
| 1201 | audio_channel_count_from_out_mask(config->channel_mask)); |
| 1202 | if (deviceDesc != nullptr && (tryDirectForFlags || tryDirectForChannelMask)) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1203 | audio_io_handle_t newOutput; |
| 1204 | status = openDirectOutput( |
| 1205 | *stream, session, config, |
| 1206 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
| 1207 | DeviceVector(deviceDesc), &newOutput); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1208 | if (status == NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1209 | policyDesc = mOutputs.valueFor(newOutput); |
| 1210 | primaryMix->setOutput(policyDesc); |
Dean Wheatley | ecbf2ee | 2022-03-04 10:51:36 +1100 | [diff] [blame] | 1211 | } else if (tryDirectForFlags) { |
| 1212 | policyDesc = nullptr; |
| 1213 | } // otherwise use primary if available. |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1214 | } |
| 1215 | if (policyDesc != nullptr) { |
| 1216 | policyDesc->mPolicyMix = primaryMix; |
| 1217 | *output = policyDesc->mIoHandle; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1218 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1219 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1220 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1221 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1222 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1223 | } else { |
| 1224 | *outputType = API_OUTPUT_LEGACY; |
| 1225 | } |
| 1226 | return NO_ERROR; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1227 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1228 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1229 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1230 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1231 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1232 | return BAD_VALUE; |
| 1233 | } |
| 1234 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1235 | // in order to let the choice of the order to future vendor engine |
| 1236 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1237 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1238 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1239 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1242 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1243 | // chosen by the engine if not. |
| 1244 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1245 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1246 | // 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] | 1247 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1248 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1249 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1250 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1251 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1252 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1253 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1254 | } |
| 1255 | } |
| 1256 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1257 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1258 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1259 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1260 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1261 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1262 | if (!msdDevices.isEmpty()) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1263 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags, isSpatialized); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1264 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1265 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1266 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1267 | } else { |
| 1268 | *output = AUDIO_IO_HANDLE_NONE; |
| 1269 | } |
| 1270 | } |
| 1271 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1272 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1273 | flags, isSpatialized, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1274 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1275 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1276 | AudioProfileVector profiles; |
| 1277 | status_t ret = getProfilesForDevices(outputDevices, profiles, *flags, false /*isInput*/); |
| 1278 | if (ret == NO_ERROR && !profiles.empty()) { |
| 1279 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 1280 | : *profiles[0]->getChannels().begin(); |
| 1281 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 1282 | : *profiles[0]->getSampleRates().begin(); |
| 1283 | config->format = profiles[0]->getFormat(); |
| 1284 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1285 | return INVALID_OPERATION; |
| 1286 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1287 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1288 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1289 | for (auto &outputDevice : outputDevices) { |
| 1290 | if (outputDevice->getId() == getConfig().getDefaultOutputDevice()->getId()) { |
| 1291 | *selectedDeviceId = outputDevice->getId(); |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1295 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1296 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1297 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1298 | } else { |
| 1299 | *outputType = API_OUTPUT_LEGACY; |
| 1300 | } |
| 1301 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1302 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1303 | |
| 1304 | return NO_ERROR; |
| 1305 | } |
| 1306 | |
| 1307 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1308 | audio_io_handle_t *output, |
| 1309 | audio_session_t session, |
| 1310 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1311 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 1312 | audio_config_t *config, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1313 | audio_output_flags_t *flags, |
| 1314 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1315 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1316 | std::vector<audio_io_handle_t> *secondaryOutputs, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1317 | output_type_t *outputType, |
| 1318 | bool *isSpatialized) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1319 | { |
| 1320 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1321 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1322 | return INVALID_OPERATION; |
| 1323 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1324 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1325 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1326 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1327 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1328 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1329 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1330 | const sp<DeviceDescriptor> requestedDevice = |
| 1331 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1332 | |
| 1333 | // Prevent from storing invalid requested device id in clients |
| 1334 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1335 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1336 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1337 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1338 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1339 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1340 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType, isSpatialized); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1341 | if (status != NO_ERROR) { |
| 1342 | return status; |
| 1343 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1344 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1345 | if (secondaryOutputs != nullptr) { |
| 1346 | for (auto &secondaryMix : secondaryMixes) { |
| 1347 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1348 | if (outputDesc != nullptr && |
| 1349 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1350 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1351 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1352 | } |
| 1353 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1354 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1355 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1356 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1357 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1358 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1359 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1360 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1361 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1362 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1363 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1364 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1365 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1366 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1367 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1368 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1369 | std::move(weakSecondaryOutputDescs), |
| 1370 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1371 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1372 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1373 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1374 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1375 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1376 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1379 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1380 | audio_session_t session, |
| 1381 | const audio_config_t *config, |
| 1382 | audio_output_flags_t flags, |
| 1383 | const DeviceVector &devices, |
| 1384 | audio_io_handle_t *output) { |
| 1385 | |
| 1386 | *output = AUDIO_IO_HANDLE_NONE; |
| 1387 | |
| 1388 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1389 | // and not explicitly requested |
| 1390 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1391 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1392 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1393 | return NAME_NOT_FOUND; |
| 1394 | } |
| 1395 | |
| 1396 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1397 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1398 | // when audioflinger detects there is an active non offloadable effect. |
| 1399 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1400 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1401 | // in the background. |
| 1402 | sp<IOProfile> profile; |
| 1403 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1404 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1405 | profile = getProfileForOutput( |
| 1406 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1407 | flags, true /* directOnly */); |
| 1408 | } |
| 1409 | |
| 1410 | if (profile == nullptr) { |
| 1411 | return NAME_NOT_FOUND; |
| 1412 | } |
| 1413 | |
| 1414 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1415 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1416 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1417 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1418 | // reuse direct output if currently open by the same client |
| 1419 | // and configured with same parameters |
| 1420 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1421 | (config->format == desc->getFormat()) && |
| 1422 | (config->channel_mask == desc->getChannelMask()) && |
| 1423 | (session == desc->mDirectClientSession)) { |
| 1424 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1425 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1426 | mOutputs.keyAt(i), session); |
| 1427 | *output = mOutputs.keyAt(i); |
| 1428 | return NO_ERROR; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | if (!profile->canOpenNewIo()) { |
| 1434 | return NAME_NOT_FOUND; |
| 1435 | } |
| 1436 | |
| 1437 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1438 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 1439 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1440 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1441 | // 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] | 1442 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1443 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1444 | status_t status = |
| 1445 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, flags, output); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1446 | |
| 1447 | // only accept an output with the requested parameters |
| 1448 | if (status != NO_ERROR || |
| 1449 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1450 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1451 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1452 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1453 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1454 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1455 | config->channel_mask, outputDesc->getChannelMask()); |
| 1456 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1457 | outputDesc->close(); |
| 1458 | } |
| 1459 | // fall back to mixer output if possible when the direct output could not be open |
| 1460 | if (audio_is_linear_pcm(config->format) && |
| 1461 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1462 | return NAME_NOT_FOUND; |
| 1463 | } |
| 1464 | *output = AUDIO_IO_HANDLE_NONE; |
| 1465 | return BAD_VALUE; |
| 1466 | } |
| 1467 | outputDesc->mDirectOpenCount = 1; |
| 1468 | outputDesc->mDirectClientSession = session; |
| 1469 | |
| 1470 | addOutput(*output, outputDesc); |
| 1471 | mPreviousOutputs = mOutputs; |
| 1472 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1473 | mpClientInterface->onAudioPortListUpdate(); |
| 1474 | return NO_ERROR; |
| 1475 | } |
| 1476 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1477 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1478 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1479 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1480 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1481 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1482 | audio_output_flags_t *flags, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1483 | bool *isSpatialized, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1484 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1485 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1486 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1487 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1488 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1489 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1490 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1491 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1492 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1493 | // open a direct output if required by specified parameters |
| 1494 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1495 | // and all common behaviors are driven by checking only the direct flag |
| 1496 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1497 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1498 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1499 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1500 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1501 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1502 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1503 | |
| 1504 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1505 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1506 | // only allow deep buffering for music stream type |
| 1507 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1508 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1509 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1510 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1511 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1512 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1513 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1514 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1515 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1516 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1517 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1518 | audio_is_linear_pcm(config->format) && |
| 1519 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1520 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1521 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1522 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1523 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1524 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1525 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1526 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1527 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1528 | } |
| 1529 | |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1530 | *isSpatialized = false; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1531 | if (mSpatializerOutput != nullptr |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 1532 | && canBeSpatializedInt(attr, config, devices.toTypeAddrVector())) { |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 1533 | *isSpatialized = true; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1534 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1535 | } |
| 1536 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1537 | audio_config_t directConfig = *config; |
| 1538 | directConfig.channel_mask = channelMask; |
| 1539 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1540 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1541 | return output; |
| 1542 | } |
| 1543 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1544 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1545 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1546 | 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] | 1547 | return AUDIO_IO_HANDLE_NONE; |
| 1548 | } |
| 1549 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1550 | // ignoring channel mask due to downmix capability in mixer |
| 1551 | |
| 1552 | // open a non direct output |
| 1553 | |
| 1554 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1555 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1556 | // get which output is suitable for the specified stream. The actual |
| 1557 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1558 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1559 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1560 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1561 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1562 | output = selectOutput( |
| 1563 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1564 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1565 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1566 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1567 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1568 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1569 | return output; |
| 1570 | } |
| 1571 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1572 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1573 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1574 | mAvailableInputDevices); |
| 1575 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1576 | } |
| 1577 | |
| 1578 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1579 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1580 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1581 | } |
| 1582 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1583 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1584 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1585 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1586 | if (msdModule != 0) { |
| 1587 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1588 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1589 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1590 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1591 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1592 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1593 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1594 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | return msdPatches; |
| 1599 | } |
| 1600 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1601 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1602 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1603 | if (index < 0) { |
| 1604 | return false; |
| 1605 | } |
| 1606 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1607 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1608 | if (msdModule == nullptr) { |
| 1609 | return false; |
| 1610 | } |
| 1611 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1612 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1613 | return true; |
| 1614 | } |
| 1615 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1616 | if (index < 0) { |
| 1617 | return false; |
| 1618 | } |
| 1619 | return true; |
| 1620 | } |
| 1621 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1622 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1623 | const InputProfileCollection &inputProfiles, |
| 1624 | const OutputProfileCollection &outputProfiles, |
| 1625 | const sp<DeviceDescriptor> &sourceDevice, |
| 1626 | const sp<DeviceDescriptor> &sinkDevice, |
| 1627 | AudioProfileVector& sourceProfiles, |
| 1628 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1629 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1630 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1631 | return NO_INIT; |
| 1632 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1633 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1634 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1635 | return NO_INIT; |
| 1636 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1637 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1638 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1639 | inProfile->supportsDevice(sourceDevice)) { |
| 1640 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1641 | } |
| 1642 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1643 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1644 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1645 | outProfile->supportsDevice(sinkDevice)) { |
| 1646 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1647 | } |
| 1648 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1649 | return NO_ERROR; |
| 1650 | } |
| 1651 | |
| 1652 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1653 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1654 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1655 | { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1656 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1657 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, |
| 1658 | msdCompressedFormatsOrder, msdSurroundChannelMasksOrder, |
| 1659 | true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1660 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1661 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1662 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1663 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1664 | } |
| 1665 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1666 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1667 | sinkConfig->format = bestSinkConfig.format; |
| 1668 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1669 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1670 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1671 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1672 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1673 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1674 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1675 | // raw and IEC61937 framed streams. |
| 1676 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1677 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1678 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1679 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1680 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1681 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1682 | sourceConfig->format = bestSinkConfig.format; |
| 1683 | // Copy input stream directly without any processing (e.g. resampling). |
| 1684 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1685 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1686 | if (hwAvSync) { |
| 1687 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1688 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1689 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1690 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1691 | } |
| 1692 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1693 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1694 | sinkConfig->config_mask |= config_mask; |
| 1695 | sourceConfig->config_mask |= config_mask; |
| 1696 | return NO_ERROR; |
| 1697 | } |
| 1698 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1699 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 1700 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1701 | { |
| 1702 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1703 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1704 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 1705 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 1706 | if (deviceModule == nullptr) { |
| 1707 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 1708 | return patchBuilder; |
| 1709 | } |
| 1710 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 1711 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 1712 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 1713 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 1714 | |
| 1715 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 1716 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 1717 | device : getMsdAudioOutDevices().itemAt(0); |
| 1718 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 1719 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1720 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1721 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1722 | AudioProfileVector sourceProfiles; |
| 1723 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1724 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1725 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1726 | for (auto hwAvSync : { true, false }) { |
| 1727 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 1728 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 1729 | continue; |
| 1730 | } |
| 1731 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 1732 | &sinkConfig) == NO_ERROR) { |
| 1733 | // Found a matching config. Re-create PatchBuilder with this config. |
| 1734 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1735 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1736 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1737 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1738 | " supporting PCM format conversion.", __func__); |
| 1739 | return patchBuilder; |
| 1740 | } |
| 1741 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1742 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1743 | DeviceVector devices; |
| 1744 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1745 | devices.add(*outputDevices); |
| 1746 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1747 | // Use media strategy for unspecified output device. This should only |
| 1748 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1749 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1750 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1751 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1752 | 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] | 1753 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1754 | std::vector<PatchBuilder> patchesToCreate; |
| 1755 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1756 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1757 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1758 | } |
| 1759 | // Retain only the MSD patches associated with outputDevices request. |
| 1760 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1761 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1762 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1763 | auto retainedPatch = false; |
| 1764 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1765 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1766 | patchesToRemove.removeItemsAt(i); |
| 1767 | retainedPatch = true; |
| 1768 | break; |
| 1769 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1770 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1771 | if (retainedPatch) { |
| 1772 | it = patchesToCreate.erase(it); |
| 1773 | continue; |
| 1774 | } |
| 1775 | ++it; |
| 1776 | } |
| 1777 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1778 | return NO_ERROR; |
| 1779 | } |
| 1780 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1781 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1782 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1783 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1784 | status_t status = NO_ERROR; |
| 1785 | for (const auto &p : patchesToCreate) { |
| 1786 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1787 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1788 | char message[256]; |
| 1789 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1790 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1791 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1792 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1793 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1794 | if (currStatus == NO_ERROR) { |
| 1795 | ALOGD("%s", message); |
| 1796 | } else { |
| 1797 | ALOGE("%s", message); |
| 1798 | if (status == NO_ERROR) { |
| 1799 | status = currStatus; |
| 1800 | } |
| 1801 | } |
| 1802 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1803 | return status; |
| 1804 | } |
| 1805 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1806 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 1807 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1808 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1809 | const auto& patch = msdPatches[i]; |
| 1810 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1811 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1812 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1813 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1814 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1815 | break; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | } |
| 1820 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1821 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
| 1822 | DeviceVector devicesToCheck = mOutputDevicesAll.getDevicesFromDeviceTypeAddrVec(devices); |
| 1823 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 1824 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1825 | const auto& patch = msdPatches[i]; |
| 1826 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1827 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1828 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1829 | const auto& foundDevice = devicesToCheck.getDevice( |
| 1830 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 1831 | if (foundDevice != nullptr) { |
| 1832 | devicesToCheck.remove(foundDevice); |
| 1833 | if (devicesToCheck.isEmpty()) { |
| 1834 | return true; |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | return false; |
| 1841 | } |
| 1842 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1843 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1844 | audio_output_flags_t flags, |
| 1845 | audio_format_t format, |
| 1846 | audio_channel_mask_t channelMask, |
| 1847 | uint32_t samplingRate, |
| 1848 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1849 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1850 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 1851 | "%s called with format %#x", __func__, format); |
| 1852 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1853 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 1854 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 1855 | // haptic-generating effect attached to is in given outputs. |
| 1856 | if (sessionId != AUDIO_SESSION_NONE) { |
| 1857 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 1858 | sessionId, FX_IID_HAPTICGENERATOR); |
| 1859 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 1860 | return hapticGeneratingOutput; |
| 1861 | } |
| 1862 | } |
| 1863 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1864 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 1865 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 1866 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1867 | |
| 1868 | // Flags expressing a functional request: must be honored in priority over |
| 1869 | // other criteria |
| 1870 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 1871 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 1872 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 1873 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1874 | // Flags expressing a performance request: have lower priority than serving |
| 1875 | // requested sampling rate or channel mask |
| 1876 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 1877 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 1878 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 1879 | |
| 1880 | const audio_output_flags_t functionalFlags = |
| 1881 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 1882 | const audio_output_flags_t performanceFlags = |
| 1883 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 1884 | |
| 1885 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 1886 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1887 | // 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] | 1888 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1889 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1890 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1891 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1892 | // with tiebreak preferring the minimum number of extra functional flags |
| 1893 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1894 | // 3: the output supporting the exact channel mask |
| 1895 | // 4: the output with a higher channel count than requested |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 1896 | // 5: the output with the highest sampling rate if the requested sample rate is |
| 1897 | // greater than default sampling rate |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1898 | // 6: the output with the highest number of requested performance flags |
| 1899 | // 7: the output with the bit depth the closest to the requested one |
| 1900 | // 8: the primary output |
| 1901 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1902 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1903 | // matching criteria values in priority order for best matching output so far |
| 1904 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1905 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1906 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 1907 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1908 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1909 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1910 | for (audio_io_handle_t output : outputs) { |
| 1911 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1912 | // matching criteria values in priority order for current output |
| 1913 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1914 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1915 | if (outputDesc->isDuplicated()) { |
| 1916 | continue; |
| 1917 | } |
| 1918 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 1919 | continue; |
| 1920 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1921 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1922 | // If haptic channel is specified, use the haptic output if present. |
| 1923 | // When using haptic output, same audio format and sample rate are required. |
| 1924 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1925 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1926 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 1927 | continue; |
| 1928 | } |
| 1929 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1930 | && format == outputDesc->getFormat() |
| 1931 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1932 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 1933 | } |
| 1934 | |
| 1935 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1936 | const int matchingFunctionalFlags = |
| 1937 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 1938 | const int totalFunctionalFlags = |
| 1939 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 1940 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 1941 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1942 | |
| 1943 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1944 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 1945 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1946 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 1947 | channelCount <= outputChannelCount) { |
| 1948 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1949 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 1950 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1951 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1952 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1953 | currentMatchCriteria[3] = outputChannelCount; |
| 1954 | } |
| 1955 | |
| 1956 | // sampling rate match |
jiabin | b12a6da | 2022-06-03 20:48:18 +0000 | [diff] [blame] | 1957 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1958 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | // performance flags match |
| 1962 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 1963 | |
| 1964 | // format match |
| 1965 | if (format != AUDIO_FORMAT_INVALID) { |
| 1966 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1967 | PolicyAudioPort::kFormatDistanceMax - |
| 1968 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | // primary output match |
| 1972 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 1973 | |
| 1974 | // compare match criteria by priority then value |
| 1975 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 1976 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 1977 | bestMatchCriteria = currentMatchCriteria; |
| 1978 | bestOutput = output; |
| 1979 | |
| 1980 | std::stringstream result; |
| 1981 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 1982 | std::ostream_iterator<int>(result, " ")); |
| 1983 | ALOGV("%s new bestOutput %d criteria %s", |
| 1984 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1988 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1991 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1992 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1993 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1994 | |
| 1995 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1996 | if (outputDesc == 0) { |
| 1997 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1998 | return BAD_VALUE; |
| 1999 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2000 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2001 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2002 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2003 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2004 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2005 | status_t status = outputDesc->start(); |
| 2006 | if (status != NO_ERROR) { |
| 2007 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2010 | uint32_t delayMs; |
| 2011 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2012 | |
| 2013 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2014 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2015 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2016 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2017 | if (delayMs != 0) { |
| 2018 | usleep(delayMs * 1000); |
| 2019 | } |
| 2020 | |
| 2021 | return status; |
| 2022 | } |
| 2023 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2024 | bool AudioPolicyManager::isLeUnicastActive() const { |
| 2025 | if (isInCall()) { |
| 2026 | return true; |
| 2027 | } |
| 2028 | return isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet()); |
| 2029 | } |
| 2030 | |
| 2031 | bool AudioPolicyManager::isAnyDeviceTypeActive(const DeviceTypeSet& deviceTypes) const { |
| 2032 | if (mAvailableOutputDevices.getDevicesFromTypes(deviceTypes).isEmpty()) { |
| 2033 | return false; |
| 2034 | } |
| 2035 | bool active = mOutputs.isAnyDeviceTypeActive(deviceTypes); |
| 2036 | ALOGV("%s active %d", __func__, active); |
| 2037 | return active; |
| 2038 | } |
| 2039 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2040 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2041 | const sp<TrackClientDescriptor>& client, |
| 2042 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2043 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2044 | // cannot start playback of STREAM_TTS if any other output is being used |
| 2045 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2046 | |
| 2047 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2048 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2049 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2050 | auto clientStrategy = client->strategy(); |
| 2051 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2052 | if (stream == AUDIO_STREAM_TTS) { |
| 2053 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2054 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 2055 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2056 | return INVALID_OPERATION; |
| 2057 | } else { |
| 2058 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2059 | } |
| 2060 | } else { |
| 2061 | // some playback other than beacon starts |
| 2062 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2063 | } |
| 2064 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2065 | // 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] | 2066 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2067 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2068 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2069 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2070 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2071 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2072 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2073 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2074 | address = policyMix->mDeviceAddress.string(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2075 | 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] | 2076 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2077 | } else { |
| 2078 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2079 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2080 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2081 | AUDIO_FORMAT_DEFAULT); |
| 2082 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2083 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2086 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2087 | // It covers a common case when there is no materially active audio |
| 2088 | // and muting would result in unnecessary delay and dropped audio. |
| 2089 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2090 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2091 | bool wasLeUnicastActive = isLeUnicastActive(); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2092 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2093 | // increment usage count for this stream on the requested output: |
| 2094 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2095 | // 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] | 2096 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2097 | |
| 2098 | if (client->hasPreferredDevice(true)) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2099 | if (outputDesc->clientsList(true /*activeOnly*/).size() == 1 && |
| 2100 | client->isPreferredDeviceForExclusiveUse()) { |
| 2101 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2102 | devices = DeviceVector( |
| 2103 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2104 | } else { |
| 2105 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2106 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2107 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2108 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2109 | } |
| 2110 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2111 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2112 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2113 | selectOutputForMusicEffects(); |
| 2114 | } |
| 2115 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2116 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2117 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2118 | if (devices.isEmpty()) { |
| 2119 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2120 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2121 | bool shouldWait = |
| 2122 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2123 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2124 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2125 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2126 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2127 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2128 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2129 | // An output has a shared device if |
| 2130 | // - managed by the same hw module |
| 2131 | // - supports the currently selected device |
| 2132 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2133 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2134 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2135 | // force a device change if any other output is: |
| 2136 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2137 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2138 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2139 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2140 | // 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] | 2141 | // change the device currently selected by the other output. |
| 2142 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2143 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2144 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2145 | force = true; |
| 2146 | } |
| 2147 | // 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] | 2148 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2149 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2150 | const uint32_t latencyMs = desc->latency(); |
| 2151 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2152 | |
| 2153 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2154 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2155 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2156 | |
| 2157 | // Require mute check if another output is on a shared device |
| 2158 | // and currently active to have proper drain and avoid pops. |
| 2159 | // Note restoring AudioTracks onto this output needs to invoke |
| 2160 | // a volume ramp if there is no mute. |
| 2161 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2162 | } |
| 2163 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2164 | |
| 2165 | const uint32_t muteWaitMs = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2166 | setOutputDevices(outputDesc, devices, force, 0, NULL, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2167 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2168 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2169 | auto &curves = getVolumeCurves(client->attributes()); |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2170 | if (NO_ERROR != checkAndSetVolume(curves, client->volumeSource(), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2171 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2172 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2173 | outputDesc->devices().types(), 0 /*delay*/, |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 2174 | outputDesc->useHwGain() /*force*/)) { |
| 2175 | // request AudioService to reinitialize the volume curves asynchronously |
| 2176 | ALOGE("checkAndSetVolume failed, requesting volume range init"); |
| 2177 | mpClientInterface->onVolumeRangeInitRequest(); |
| 2178 | }; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2179 | |
| 2180 | // update the outputs if starting an output with a stream that can affect notification |
| 2181 | // routing |
| 2182 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2183 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2184 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2185 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2186 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 2187 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2188 | |
| 2189 | if (waitMs > muteWaitMs) { |
| 2190 | *delayMs = waitMs - muteWaitMs; |
| 2191 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2192 | |
| 2193 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2194 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2195 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2196 | // change occurs after the MixerThread starts and causes a stream volume |
| 2197 | // glitch. |
| 2198 | // |
| 2199 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2200 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2201 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2202 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2203 | mEngine->getForceUse( |
| 2204 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2205 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2206 | } |
| 2207 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2208 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2209 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2210 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2211 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2212 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2213 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2214 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2215 | "remote-submix", |
| 2216 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2219 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, *delayMs); |
| 2220 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2221 | return NO_ERROR; |
| 2222 | } |
| 2223 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2224 | void AudioPolicyManager::checkLeBroadcastRoutes(bool wasUnicastActive, |
| 2225 | sp<SwAudioOutputDescriptor> ignoredOutput, uint32_t delayMs) { |
| 2226 | bool isUnicastActive = isLeUnicastActive(); |
| 2227 | |
| 2228 | if (wasUnicastActive != isUnicastActive) { |
| 2229 | //reroute all outputs routed to LE broadcast if LE unicast activy changed on any output |
| 2230 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2231 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2232 | if (desc != ignoredOutput && desc->isActive() |
| 2233 | && ((isUnicastActive && |
| 2234 | !desc->devices(). |
| 2235 | getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST).isEmpty()) |
| 2236 | || (wasUnicastActive && |
| 2237 | !desc->devices().getDevicesFromTypes( |
| 2238 | getAudioDeviceOutLeAudioUnicastSet()).isEmpty()))) { |
| 2239 | DeviceVector newDevices = getNewOutputDevices(desc, false /*fromCache*/); |
| 2240 | bool force = desc->devices() != newDevices; |
| 2241 | setOutputDevices(desc, newDevices, force, delayMs); |
| 2242 | // re-apply device specific volume if not done by setOutputDevice() |
| 2243 | if (!force) { |
| 2244 | applyStreamVolumes(desc, newDevices.types(), delayMs); |
| 2245 | } |
| 2246 | } |
| 2247 | } |
| 2248 | } |
| 2249 | } |
| 2250 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2251 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2252 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2253 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2254 | |
| 2255 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2256 | if (outputDesc == 0) { |
| 2257 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2258 | return BAD_VALUE; |
| 2259 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2260 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2261 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2262 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2263 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2264 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2265 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2266 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2267 | if (status == NO_ERROR ) { |
| 2268 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2269 | } |
| 2270 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2273 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2274 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2275 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2276 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2277 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2278 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2279 | bool wasLeUnicastActive = isLeUnicastActive(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2280 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2281 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2282 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2283 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2284 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2285 | // Automatically disable the remote submix input when output is stopped on a |
| 2286 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2287 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2288 | if (isSingleDeviceType( |
| 2289 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2290 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2291 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2292 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2293 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2294 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2295 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2296 | } |
| 2297 | } |
| 2298 | bool forceDeviceUpdate = false; |
| 2299 | if (client->hasPreferredDevice(true)) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2300 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2301 | forceDeviceUpdate = true; |
| 2302 | } |
| 2303 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2304 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2305 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2306 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2307 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2308 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2309 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2310 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2311 | |
| 2312 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2313 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2314 | // volume group(s), force reapply volume |
| 2315 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2316 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2317 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2318 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2319 | // the track stop() command is received and at that time the audio track buffer can |
| 2320 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2321 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2322 | // audio path (audio DSP, CODEC ...) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2323 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2, |
| 2324 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2325 | |
| 2326 | // force restoring the device selection on other active outputs if it differs from the |
| 2327 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2328 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2329 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2330 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2331 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2332 | desc->isActive() && |
| 2333 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2334 | (newDevices != desc->devices())) { |
| 2335 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2336 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2337 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2338 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 2339 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2340 | // re-apply device specific volume if not done by setOutputDevice() |
| 2341 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2342 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2343 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2347 | handleNotificationRoutingForStream(stream); |
| 2348 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2349 | |
| 2350 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2351 | 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] | 2352 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2353 | } |
| 2354 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2355 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2356 | selectOutputForMusicEffects(); |
| 2357 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 2358 | |
| 2359 | checkLeBroadcastRoutes(wasLeUnicastActive, outputDesc, outputDesc->latency()*2); |
| 2360 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2361 | return NO_ERROR; |
| 2362 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2363 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2364 | return INVALID_OPERATION; |
| 2365 | } |
| 2366 | } |
| 2367 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2368 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2369 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2370 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2371 | |
| 2372 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2373 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2374 | // If an output descriptor is closed due to a device routing change, |
| 2375 | // then there are race conditions with releaseOutput from tracks |
| 2376 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2377 | // destroyed shortly thereafter. |
| 2378 | // |
| 2379 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2380 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2381 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2382 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2383 | |
| 2384 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2385 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2386 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2387 | if (outputDesc->isClientActive(client)) { |
| 2388 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2389 | stopOutput(portId); |
| 2390 | } |
| 2391 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2392 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2393 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2394 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2395 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2396 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2397 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2398 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2399 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2400 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2401 | } |
| 2402 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2403 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2404 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2405 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2406 | // The output is pending reopened to query dynamic profiles and |
| 2407 | // there is no active clients |
| 2408 | closeOutput(outputDesc->mIoHandle); |
| 2409 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2410 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2411 | if (newOutputDesc == nullptr) { |
| 2412 | ALOGE("%s failed to open output", __func__); |
| 2413 | } |
| 2414 | return true; |
| 2415 | } |
| 2416 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2419 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2420 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2421 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2422 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2423 | const AttributionSourceState& attributionSource, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2424 | audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2425 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2426 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2427 | input_type_t *inputType, |
| 2428 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2429 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2430 | 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] | 2431 | "flags %#x attributes=%s requested device ID %d", |
| 2432 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2433 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2434 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2435 | status_t status = NO_ERROR; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2436 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2437 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2438 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2439 | sp<AudioInputDescriptor> inputDesc; |
| 2440 | sp<RecordClientDescriptor> clientDesc; |
| 2441 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2442 | 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] | 2443 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2444 | |
| 2445 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2446 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2447 | return INVALID_OPERATION; |
| 2448 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2449 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2450 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2451 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2452 | } |
| 2453 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2454 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2455 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2456 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2457 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2458 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2459 | // possible |
| 2460 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2461 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2462 | ssize_t index = mInputs.indexOfKey(*input); |
| 2463 | if (index < 0) { |
| 2464 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2465 | status = BAD_VALUE; |
| 2466 | goto error; |
| 2467 | } |
| 2468 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2469 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2470 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2471 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2472 | status = BAD_VALUE; |
| 2473 | goto error; |
| 2474 | } |
| 2475 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2476 | // 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] | 2477 | // corresponds to a new client and is only permitted from the same UID. |
| 2478 | // 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] | 2479 | if (clients.size() > 1) { |
| 2480 | for (const auto& client : clients) { |
| 2481 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2482 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2483 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2484 | // to an actual client |
| 2485 | if (client == *clients.cbegin()) { |
| 2486 | continue; |
| 2487 | } |
| 2488 | if (uid != client->uid() && !client->isSilenced()) { |
| 2489 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2490 | uid, client->portId(), client->uid()); |
| 2491 | status = INVALID_OPERATION; |
| 2492 | goto error; |
| 2493 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2494 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2495 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2496 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2497 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2498 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2499 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2500 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | *input = AUDIO_IO_HANDLE_NONE; |
| 2504 | *inputType = API_INPUT_INVALID; |
| 2505 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2506 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
| 2507 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { |
| 2508 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2509 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2510 | ALOGW("%s could not find input mix for attr %s", |
| 2511 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2512 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2513 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2514 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2515 | String8(attr->tags + strlen("addr=")), |
| 2516 | AUDIO_FORMAT_DEFAULT); |
| 2517 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2518 | 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] | 2519 | __func__, attributes.source, attributes.tags); |
| 2520 | status = BAD_VALUE; |
| 2521 | goto error; |
| 2522 | } |
| 2523 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2524 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2525 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2526 | } else { |
| 2527 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2528 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2529 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2530 | if (explicitRoutingDevice != nullptr) { |
| 2531 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2532 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2533 | // Prevent from storing invalid requested device id in clients |
| 2534 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 2535 | device = mEngine->getInputDeviceForAttributes(attributes, uid, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2536 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 2537 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2538 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2539 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2540 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2541 | status = BAD_VALUE; |
| 2542 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2543 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2544 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2545 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2546 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2547 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2548 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2549 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2550 | // know about it and is therefore considered "legacy" |
| 2551 | *inputType = API_INPUT_LEGACY; |
| 2552 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2553 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2554 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2555 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2556 | } else { |
| 2557 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2558 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2559 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2560 | } |
| 2561 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2562 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2563 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2564 | status = INVALID_OPERATION; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2565 | AudioProfileVector profiles; |
| 2566 | status_t ret = getProfilesForDevices( |
| 2567 | DeviceVector(device), profiles, flags, true /*isInput*/); |
| 2568 | if (ret == NO_ERROR && !profiles.empty()) { |
| 2569 | config->channel_mask = profiles[0]->getChannels().empty() ? config->channel_mask |
| 2570 | : *profiles[0]->getChannels().begin(); |
| 2571 | config->sample_rate = profiles[0]->getSampleRates().empty() ? config->sample_rate |
| 2572 | : *profiles[0]->getSampleRates().begin(); |
| 2573 | config->format = profiles[0]->getFormat(); |
| 2574 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2575 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2576 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2577 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2578 | exit: |
| 2579 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2580 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2581 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2582 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2583 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2584 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2585 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2586 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2587 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2588 | requestedDeviceId, attributes.source, flags, |
| 2589 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2590 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2591 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2592 | |
| 2593 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2594 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2595 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2596 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2597 | |
| 2598 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2599 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2603 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2604 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2605 | const audio_attributes_t &attributes, |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 2606 | audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2607 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2608 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2609 | { |
| 2610 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2611 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2612 | bool isSoundTrigger = false; |
| 2613 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2614 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2615 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2616 | if (index >= 0) { |
| 2617 | input = mSoundTriggerSessions.valueFor(session); |
| 2618 | isSoundTrigger = true; |
| 2619 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2620 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2621 | } else { |
| 2622 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2623 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2624 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2625 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2626 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 2629 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 2630 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 2631 | } |
| 2632 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2633 | // sampling rate and flags may be updated by getInputProfile |
| 2634 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2635 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2636 | audio_format_t profileFormat = config->format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2637 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2638 | audio_input_flags_t profileFlags = flags; |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2639 | // find a compatible input profile (not necessarily identical in parameters) |
| 2640 | sp<IOProfile> profile = getInputProfile( |
| 2641 | device, profileSamplingRate, profileFormat, profileChannelMask, profileFlags); |
| 2642 | if (profile == nullptr) { |
| 2643 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2644 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 2645 | |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2646 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2647 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2648 | if (samplingRate == 0) { |
| 2649 | samplingRate = profileSamplingRate; |
| 2650 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2651 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2652 | if (profile->getModuleHandle() == 0) { |
| 2653 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2654 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2657 | // Reuse an already opened input if a client with the same session ID already exists |
| 2658 | // on that input |
| 2659 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2660 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2661 | if (desc->mProfile != profile) { |
| 2662 | continue; |
| 2663 | } |
| 2664 | RecordClientVector clients = desc->clientsList(); |
| 2665 | for (const auto &client : clients) { |
| 2666 | if (session == client->session()) { |
| 2667 | return desc->mIoHandle; |
| 2668 | } |
| 2669 | } |
| 2670 | } |
| 2671 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2672 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2673 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2674 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2675 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2676 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2677 | continue; |
| 2678 | } |
| 2679 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2680 | // else |
| 2681 | // reuse input if active client app is not in IDLE state |
| 2682 | // |
| 2683 | RecordClientVector clients = desc->clientsList(); |
| 2684 | bool doClose = false; |
| 2685 | for (const auto& client : clients) { |
| 2686 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2687 | continue; |
| 2688 | } |
| 2689 | if (client->isSoundTrigger()) { |
| 2690 | if (session == client->session()) { |
| 2691 | return desc->mIoHandle; |
| 2692 | } |
| 2693 | continue; |
| 2694 | } |
| 2695 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2696 | return desc->mIoHandle; |
| 2697 | } |
| 2698 | doClose = true; |
| 2699 | } |
| 2700 | if (doClose) { |
| 2701 | closeInput(desc->mIoHandle); |
| 2702 | } else { |
| 2703 | i++; |
| 2704 | } |
| 2705 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2706 | } |
| 2707 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2708 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2709 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2710 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2711 | lConfig.sample_rate = profileSamplingRate; |
| 2712 | lConfig.channel_mask = profileChannelMask; |
| 2713 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2714 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2715 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2716 | |
| 2717 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2718 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2719 | (profileSamplingRate != lConfig.sample_rate) || |
| 2720 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2721 | (profileChannelMask != lConfig.channel_mask)) { |
| 2722 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2723 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2724 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2725 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2726 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2727 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2728 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2729 | } |
| 2730 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2731 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2732 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2733 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2734 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2735 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2736 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2737 | } |
| 2738 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2739 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2740 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2741 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2742 | |
| 2743 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2744 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2745 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2746 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2747 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2748 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2749 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2750 | if (client->active()) { |
| 2751 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2752 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2753 | } |
| 2754 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2755 | audio_session_t session = client->session(); |
| 2756 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2757 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2758 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2759 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2760 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2761 | status_t status = inputDesc->start(); |
| 2762 | if (status != NO_ERROR) { |
| 2763 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2764 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2765 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2766 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2767 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2768 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2769 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2770 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2771 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2772 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2773 | if (device != nullptr) { |
| 2774 | status = setInputDevice(input, device, true /* force */); |
| 2775 | } else { |
| 2776 | ALOGW("%s no new input device can be found for descriptor %d", |
| 2777 | __FUNCTION__, inputDesc->getId()); |
| 2778 | status = BAD_VALUE; |
| 2779 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2780 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2781 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2782 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2783 | // 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] | 2784 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2785 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2786 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2787 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2788 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2789 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2790 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2791 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2792 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2793 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2794 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2795 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2796 | // automatically enable the remote submix output when input is started if not |
| 2797 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2798 | // 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] | 2799 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2800 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2801 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2802 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2803 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2804 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2805 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2806 | if (address != "") { |
| 2807 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2808 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2809 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2810 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2811 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2812 | } else if (status != NO_ERROR) { |
| 2813 | // Restore client activity state. |
| 2814 | inputDesc->setClientActive(client, false); |
| 2815 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2818 | ALOGV("%s input %d source = %d status = %d exit", |
| 2819 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2820 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2821 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2822 | } |
| 2823 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2824 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2825 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2826 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2827 | |
| 2828 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2829 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2830 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2831 | return BAD_VALUE; |
| 2832 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2833 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2834 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2835 | if (!client->active()) { |
| 2836 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2837 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2838 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2839 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2840 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2841 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2842 | inputDesc->stop(); |
| 2843 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2844 | auto current_source = inputDesc->source(); |
| 2845 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 2846 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2847 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2848 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2849 | // 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] | 2850 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2851 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2852 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2853 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2854 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2855 | |
| 2856 | // automatically disable the remote submix output when input is stopped if not |
| 2857 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2858 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2859 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2860 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2861 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2862 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2863 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2864 | } |
| 2865 | if (address != "") { |
| 2866 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2867 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2868 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2869 | } |
| 2870 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2871 | resetInputDevice(input); |
| 2872 | |
| 2873 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2874 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2875 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2876 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2877 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2878 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2879 | } |
| 2880 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2881 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2882 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2885 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2886 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2887 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2888 | |
| 2889 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2890 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2891 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2892 | return; |
| 2893 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2894 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2895 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2896 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2897 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2898 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2899 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2900 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2901 | if (inputDesc->getClientCount() > 0) { |
| 2902 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2903 | return; |
| 2904 | } |
| 2905 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2906 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2907 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2908 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2911 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2912 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2913 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2914 | |
| 2915 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2916 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2920 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2921 | { |
| 2922 | stopInput(portId); |
| 2923 | releaseInput(portId); |
| 2924 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2925 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2926 | void AudioPolicyManager::checkCloseInputs() { |
| 2927 | // After connecting or disconnecting an input device, close input if: |
| 2928 | // - it has no client (was just opened to check profile) OR |
| 2929 | // - none of its supported devices are connected anymore OR |
| 2930 | // - one of its clients cannot be routed to one of its supported |
| 2931 | // devices anymore. Otherwise update device selection |
| 2932 | std::vector<audio_io_handle_t> inputsToClose; |
| 2933 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2934 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 2935 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 2936 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2937 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 2938 | } else { |
| 2939 | bool close = false; |
| 2940 | for (const auto& client : input->clientsList()) { |
| 2941 | sp<DeviceDescriptor> device = |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 2942 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid()); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2943 | if (!input->supportedDevices().contains(device)) { |
| 2944 | close = true; |
| 2945 | break; |
| 2946 | } |
| 2947 | } |
| 2948 | if (close) { |
| 2949 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 2950 | } else { |
| 2951 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 2952 | } |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | for (const audio_io_handle_t handle : inputsToClose) { |
| 2957 | ALOGV("%s closing input %d", __func__, handle); |
| 2958 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2959 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2960 | } |
| 2961 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 2962 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2963 | { |
| 2964 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 2965 | if (indexMin < 0 || indexMax < 0) { |
| 2966 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 2967 | return; |
| 2968 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2969 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2970 | |
| 2971 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2972 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2973 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2974 | continue; |
| 2975 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2976 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2977 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2978 | } |
| 2979 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2980 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2981 | int index, |
| 2982 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2983 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2984 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 2985 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 2986 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 2987 | return NO_ERROR; |
| 2988 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2989 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 2990 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2991 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2994 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2995 | int *index, |
| 2996 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2997 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2998 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 2999 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3000 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3001 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3002 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 3003 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3004 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3005 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3006 | } |
| 3007 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3008 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3009 | int index, |
| 3010 | audio_devices_t device) |
| 3011 | { |
| 3012 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3013 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 3014 | if (group == VOLUME_GROUP_NONE) { |
| 3015 | 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] | 3016 | return BAD_VALUE; |
| 3017 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3018 | 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] | 3019 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3020 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3021 | VolumeSource vs = toVolumeSource(group); |
| 3022 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 3023 | |
| 3024 | status = setVolumeCurveIndex(index, device, curves); |
| 3025 | if (status != NO_ERROR) { |
| 3026 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 3027 | return status; |
| 3028 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3029 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3030 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3031 | auto curCurvAttrs = curves.getAttributes(); |
| 3032 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 3033 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3034 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3035 | } else if (!curves.getStreamTypes().empty()) { |
| 3036 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3037 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3038 | } else { |
| 3039 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 3040 | return BAD_VALUE; |
| 3041 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3042 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 3043 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 3044 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3045 | // update volume on all outputs and streams matching the following: |
| 3046 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 3047 | // - The device (or devices) selected by the engine for this stream includes |
| 3048 | // the requested device |
| 3049 | // - For non default requested device, currently selected device on the output is either the |
| 3050 | // requested device or one of the devices selected by the engine for this stream |
| 3051 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 3052 | // no specific device volume value exists for currently selected device. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3053 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3054 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3055 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3056 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3057 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 3058 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 3059 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3060 | if (!(desc->isActive(vs) || isInCall())) { |
| 3061 | continue; |
| 3062 | } |
| 3063 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 3064 | curDevices.find(device) == curDevices.end()) { |
| 3065 | continue; |
| 3066 | } |
| 3067 | bool applyVolume = false; |
| 3068 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 3069 | curSrcDevices.insert(device); |
| 3070 | applyVolume = (curSrcDevices.find( |
| 3071 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); |
| 3072 | } else { |
| 3073 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 3074 | } |
| 3075 | if (!applyVolume) { |
| 3076 | continue; // next output |
| 3077 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3078 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 3079 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 3080 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3081 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3082 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 3083 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 3084 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3085 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 3086 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 3087 | false /*preferredDevice*/); |
| 3088 | if (activeClients.empty()) { |
| 3089 | continue; |
| 3090 | } |
| 3091 | bool isPreempted = false; |
| 3092 | bool isHigherPriority = productStrategy < strategy; |
| 3093 | for (const auto &client : activeClients) { |
| 3094 | if (isHigherPriority && (client->volumeSource() != vs)) { |
| 3095 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3096 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3097 | " higher priority source active:\n" |
| 3098 | " volumeGroup=%d attributes=%s) \n" |
| 3099 | " on output %zu, bailing out", __func__, productStrategy, |
| 3100 | group, group, toString(attributes).c_str(), |
| 3101 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3102 | applyVolume = false; |
| 3103 | isPreempted = true; |
| 3104 | break; |
| 3105 | } |
| 3106 | // However, continue for loop to ensure no higher prio clients running on output |
| 3107 | if (client->volumeSource() == vs) { |
| 3108 | applyVolume = true; |
| 3109 | } |
| 3110 | } |
| 3111 | if (isPreempted || applyVolume) { |
| 3112 | break; |
| 3113 | } |
| 3114 | } |
| 3115 | if (!applyVolume) { |
| 3116 | continue; // next output |
| 3117 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3118 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3119 | //FIXME: workaround for truncated touch sounds |
| 3120 | // delayed volume change for system stream to be removed when the problem is |
| 3121 | // handled by system UI |
| 3122 | status_t volStatus = checkAndSetVolume( |
| 3123 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3124 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3125 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3126 | if (volStatus != NO_ERROR) { |
| 3127 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3128 | } |
| 3129 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3130 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3131 | return status; |
| 3132 | } |
| 3133 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3134 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3135 | audio_devices_t device, |
| 3136 | IVolumeCurves &volumeCurves) |
| 3137 | { |
| 3138 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3139 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3140 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3141 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3142 | (index > volumeCurves.getVolumeIndexMax())) { |
| 3143 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 3144 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 3145 | return BAD_VALUE; |
| 3146 | } |
| 3147 | if (!audio_is_output_device(device)) { |
| 3148 | return BAD_VALUE; |
| 3149 | } |
| 3150 | |
| 3151 | // Force max volume if stream cannot be muted |
| 3152 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3153 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3154 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3155 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3156 | return NO_ERROR; |
| 3157 | } |
| 3158 | |
| 3159 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3160 | int &index, |
| 3161 | audio_devices_t device) |
| 3162 | { |
| 3163 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3164 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3165 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3166 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3167 | deviceTypes = mEngine->getOutputDevicesForAttributes( |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3168 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3169 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3170 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3174 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3175 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3176 | { |
Mikhail Naganov | bb990f2 | 2022-06-15 00:46:43 +0000 | [diff] [blame] | 3177 | if (!isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3178 | return BAD_VALUE; |
| 3179 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3180 | index = curves.getVolumeIndex(deviceTypes); |
| 3181 | 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] | 3182 | return NO_ERROR; |
| 3183 | } |
| 3184 | |
| 3185 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3186 | int &index) |
| 3187 | { |
| 3188 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3189 | return NO_ERROR; |
| 3190 | } |
| 3191 | |
| 3192 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3193 | int &index) |
| 3194 | { |
| 3195 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3196 | return NO_ERROR; |
| 3197 | } |
| 3198 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3199 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3200 | { |
| 3201 | // select one output among several suitable for global effects. |
| 3202 | // The priority is as follows: |
| 3203 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3204 | // AudioFlinger will invalidate the track and the offloaded output |
| 3205 | // will be closed causing the effect to be moved to a PCM output. |
| 3206 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3207 | // 3: The primary output |
| 3208 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3209 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3210 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3211 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3212 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3213 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3214 | if (outputs.size() == 0) { |
| 3215 | return AUDIO_IO_HANDLE_NONE; |
| 3216 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3217 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3218 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3219 | bool activeOnly = true; |
| 3220 | |
| 3221 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3222 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 3223 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3224 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3225 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3226 | for (audio_io_handle_t output : outputs) { |
| 3227 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3228 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3229 | continue; |
| 3230 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3231 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 3232 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3233 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3234 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3235 | } |
| 3236 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3237 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3238 | } |
| 3239 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3240 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3244 | output = outputOffloaded; |
| 3245 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3246 | output = outputDeepBuffer; |
| 3247 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3248 | output = outputPrimary; |
| 3249 | } else { |
| 3250 | output = outputs[0]; |
| 3251 | } |
| 3252 | activeOnly = false; |
| 3253 | } |
| 3254 | |
| 3255 | if (output != mMusicEffectOutput) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3256 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3257 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 3258 | mMusicEffectOutput = output; |
| 3259 | } |
| 3260 | |
| 3261 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3262 | return output; |
| 3263 | } |
| 3264 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3265 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3266 | { |
| 3267 | return selectOutputForMusicEffects(); |
| 3268 | } |
| 3269 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3270 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3271 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3272 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3273 | int session, |
| 3274 | int id) |
| 3275 | { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3276 | if (session != AUDIO_SESSION_DEVICE) { |
| 3277 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3278 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3279 | index = mInputs.indexOfKey(io); |
| 3280 | if (index < 0) { |
| 3281 | ALOGW("registerEffect() unknown io %d", io); |
| 3282 | return INVALID_OPERATION; |
| 3283 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3284 | } |
| 3285 | } |
Eric Laurent | bf8f69f | 2022-03-25 17:48:38 +0100 | [diff] [blame] | 3286 | bool isMusicEffect = (session != AUDIO_SESSION_OUTPUT_STAGE) |
| 3287 | && ((strategy == streamToStrategy(AUDIO_STREAM_MUSIC) |
| 3288 | || strategy == PRODUCT_STRATEGY_NONE)); |
| 3289 | return mEffects.registerEffect(desc, io, session, id, isMusicEffect); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3290 | } |
| 3291 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3292 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3293 | { |
| 3294 | if (mEffects.getEffect(id) == nullptr) { |
| 3295 | return INVALID_OPERATION; |
| 3296 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3297 | if (mEffects.isEffectEnabled(id)) { |
| 3298 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3299 | setEffectEnabled(id, false); |
| 3300 | } |
| 3301 | return mEffects.unregisterEffect(id); |
| 3302 | } |
| 3303 | |
| 3304 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3305 | { |
| 3306 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3307 | if (effect == nullptr) { |
| 3308 | return INVALID_OPERATION; |
| 3309 | } |
| 3310 | |
| 3311 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3312 | if (status == NO_ERROR) { |
| 3313 | mInputs.trackEffectEnabled(effect, enabled); |
| 3314 | } |
| 3315 | return status; |
| 3316 | } |
| 3317 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3318 | |
| 3319 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3320 | { |
| 3321 | mEffects.moveEffects(ids, io); |
| 3322 | return NO_ERROR; |
| 3323 | } |
| 3324 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3325 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3326 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3327 | auto vs = toVolumeSource(stream, false); |
| 3328 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3332 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3333 | auto vs = toVolumeSource(stream, false); |
| 3334 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3335 | } |
| 3336 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3337 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3338 | { |
| 3339 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3340 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3341 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3342 | return true; |
| 3343 | } |
| 3344 | } |
| 3345 | return false; |
| 3346 | } |
| 3347 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3348 | // Register a list of custom mixes with their attributes and format. |
| 3349 | // When a mix is registered, corresponding input and output profiles are |
| 3350 | // added to the remote submix hw module. The profile contains only the |
| 3351 | // parameters (sampling rate, format...) specified by the mix. |
| 3352 | // The corresponding input remote submix device is also connected. |
| 3353 | // |
| 3354 | // When a remote submix device is connected, the address is checked to select the |
| 3355 | // appropriate profile and the corresponding input or output stream is opened. |
| 3356 | // |
| 3357 | // When capture starts, getInputForAttr() will: |
| 3358 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3359 | // - 2 if none found, getDeviceForInputSource() will: |
| 3360 | // - 2.1 look for a mix matching the attributes source |
| 3361 | // - 2.2 if none found, default to device selection by policy rules |
| 3362 | // At this time, the corresponding output remote submix device is also connected |
| 3363 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3364 | // after AudioTracks are invalidated |
| 3365 | // |
| 3366 | // When playback starts, getOutputForAttr() will: |
| 3367 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3368 | // - 2 if none found, look for a mix matching the attributes usage |
| 3369 | // - 3 if none found, default to device and output selection by policy rules. |
| 3370 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3371 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3372 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3373 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3374 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3375 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3376 | sp<HwModule> rSubmixModule; |
| 3377 | // examine each mix's route type |
| 3378 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3379 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3380 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3381 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3382 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3383 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3384 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3385 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3386 | break; |
| 3387 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3388 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3389 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3390 | 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] | 3391 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3392 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3393 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3394 | rSubmixModule = mHwModules.getModuleFromName( |
| 3395 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3396 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3397 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3398 | i); |
| 3399 | res = INVALID_OPERATION; |
| 3400 | break; |
| 3401 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3402 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3403 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3404 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3405 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3406 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3407 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3408 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3409 | } else { |
| 3410 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3411 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3412 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3413 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3414 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3415 | ALOGE("Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3416 | res = INVALID_OPERATION; |
| 3417 | break; |
| 3418 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3419 | audio_config_t outputConfig = mix.mFormat; |
| 3420 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3421 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3422 | // 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] | 3423 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3424 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3425 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3426 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3427 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3428 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3429 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3430 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3431 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3432 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
| 3433 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
| 3434 | mix.mDeviceType, address.string()); |
| 3435 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3436 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3437 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3438 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3439 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3440 | 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] | 3441 | i, mixes.size(), type, address.string()); |
| 3442 | |
| 3443 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3444 | mix.mDeviceType, mix.mDeviceAddress, |
| 3445 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3446 | if (device == nullptr) { |
| 3447 | res = INVALID_OPERATION; |
| 3448 | break; |
| 3449 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3450 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3451 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3452 | // First try to find an already opened output supporting the device |
| 3453 | 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] | 3454 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3455 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3456 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3457 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3458 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3459 | address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3460 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3461 | } else { |
| 3462 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3463 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3464 | } |
| 3465 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3466 | // If no output found, try to find a direct output profile supporting the device |
| 3467 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3468 | sp<HwModule> module = mHwModules[i]; |
| 3469 | for (size_t j = 0; |
| 3470 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3471 | j++) { |
| 3472 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3473 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3474 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3475 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3476 | address.string()); |
| 3477 | res = INVALID_OPERATION; |
| 3478 | } else { |
| 3479 | foundOutput = true; |
| 3480 | } |
| 3481 | } |
| 3482 | } |
| 3483 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3484 | if (res != NO_ERROR) { |
| 3485 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3486 | i, type, address.string()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3487 | res = INVALID_OPERATION; |
| 3488 | break; |
| 3489 | } else if (!foundOutput) { |
| 3490 | 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] | 3491 | i, type, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3492 | res = INVALID_OPERATION; |
| 3493 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3494 | } else { |
| 3495 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3496 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3497 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3498 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3499 | if (res != NO_ERROR) { |
| 3500 | unregisterPolicyMixes(mixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3501 | } else if (checkOutputs) { |
| 3502 | checkForDeviceAndOutputChanges(); |
| 3503 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3504 | } |
| 3505 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3509 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3510 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3511 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3512 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3513 | sp<HwModule> rSubmixModule; |
| 3514 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3515 | for (const auto& mix : mixes) { |
| 3516 | 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] | 3517 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3518 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3519 | rSubmixModule = mHwModules.getModuleFromName( |
| 3520 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3521 | if (rSubmixModule == 0) { |
| 3522 | res = INVALID_OPERATION; |
| 3523 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3524 | } |
| 3525 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3526 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3527 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3528 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3529 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3530 | res = INVALID_OPERATION; |
| 3531 | continue; |
| 3532 | } |
| 3533 | |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3534 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
| 3535 | if (getDeviceConnectionState(device, address.string()) == |
| 3536 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3537 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3538 | address.string(), "remote-submix", |
| 3539 | AUDIO_FORMAT_DEFAULT); |
| 3540 | if (res != OK) { |
| 3541 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
| 3542 | "with type %d, address %s", device, address.string()); |
| 3543 | } |
| 3544 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3545 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3546 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3547 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3548 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3549 | } 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] | 3550 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3551 | res = INVALID_OPERATION; |
| 3552 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3553 | } else { |
| 3554 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3555 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3556 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3557 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3558 | if (res == NO_ERROR && checkOutputs) { |
| 3559 | checkForDeviceAndOutputChanges(); |
| 3560 | updateCallAndOutputRouting(); |
| 3561 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3562 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3563 | } |
| 3564 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3565 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3566 | { |
| 3567 | size_t i = 0; |
| 3568 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3569 | for (const auto& fmt : mManualSurroundFormats) { |
| 3570 | if (i++ != 0) dst->append(", "); |
| 3571 | std::string sfmt; |
| 3572 | FormatConverter::toString(fmt, sfmt); |
| 3573 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3574 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3575 | } |
| 3576 | } |
| 3577 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3578 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3579 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3580 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3581 | std::function<bool(audio_devices_t)> predicate, |
| 3582 | const char *context) { |
| 3583 | for (size_t i = 0; i < devices.size(); i++) { |
| 3584 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3585 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 0e26e3f | 2020-04-29 14:24:16 -0700 | [diff] [blame] | 3586 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, true /*matchAddress*/); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3587 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3588 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3589 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3590 | return false; |
| 3591 | } |
| 3592 | } |
| 3593 | return true; |
| 3594 | } |
| 3595 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3596 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3597 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3598 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3599 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3600 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3601 | } |
| 3602 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3603 | if (res != NO_ERROR) { |
| 3604 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3605 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3606 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3607 | |
| 3608 | checkForDeviceAndOutputChanges(); |
| 3609 | updateCallAndOutputRouting(); |
| 3610 | |
| 3611 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 3615 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3616 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 3617 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3618 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3619 | __FUNCTION__, uid); |
| 3620 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3621 | } |
| 3622 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3623 | checkForDeviceAndOutputChanges(); |
| 3624 | updateCallAndOutputRouting(); |
| 3625 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3626 | return res; |
| 3627 | } |
| 3628 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3629 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3630 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 3631 | device_role_t role, |
| 3632 | const AudioDeviceTypeAddrVector &devices) { |
| 3633 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3634 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3635 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3636 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3637 | return BAD_VALUE; |
| 3638 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3639 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3640 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3641 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 3642 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3643 | return status; |
| 3644 | } |
| 3645 | |
| 3646 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3647 | |
| 3648 | bool forceVolumeReeval = false; |
| 3649 | // FIXME: workaround for truncated touch sounds |
| 3650 | // to be removed when the problem is handled by system UI |
| 3651 | uint32_t delayMs = 0; |
| 3652 | if (strategy == mCommunnicationStrategy) { |
| 3653 | forceVolumeReeval = true; |
| 3654 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3655 | updateInputRouting(); |
| 3656 | } |
| 3657 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3658 | |
| 3659 | return NO_ERROR; |
| 3660 | } |
| 3661 | |
| 3662 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs) |
| 3663 | { |
| 3664 | uint32_t waitMs = 0; |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3665 | bool wasLeUnicastActive = isLeUnicastActive(); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 3666 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3667 | // Only apply special touch sound delay once |
| 3668 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3669 | } |
| 3670 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3671 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3672 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3673 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 3674 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3675 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 3676 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 3677 | // Let's give back to engine full device choice decision however. |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3678 | bool forceRouting = !newDevices.isEmpty(); |
| 3679 | waitMs = setOutputDevices(outputDesc, newDevices, forceRouting, delayMs, nullptr, |
| 3680 | true /*requiresMuteCheck*/, |
| 3681 | !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3682 | // Only apply special touch sound delay once |
| 3683 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3684 | } |
| 3685 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 3686 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 3687 | } |
| 3688 | } |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 3689 | checkLeBroadcastRoutes(wasLeUnicastActive, nullptr, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3690 | } |
| 3691 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3692 | void AudioPolicyManager::updateInputRouting() { |
| 3693 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3694 | // Skip for hotword recording as the input device switch |
| 3695 | // is handled within sound trigger HAL |
| 3696 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 3697 | continue; |
| 3698 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3699 | auto newDevice = getNewInputDevice(activeDesc); |
| 3700 | // Force new input selection if the new device can not be reached via current input |
| 3701 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 3702 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 3703 | } else { |
| 3704 | closeInput(activeDesc->mIoHandle); |
| 3705 | } |
| 3706 | } |
| 3707 | } |
| 3708 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3709 | status_t AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 3710 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3711 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3712 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3713 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3714 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3715 | if (status != NO_ERROR) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3716 | ALOGV("Engine could not remove preferred device for strategy %d status %d", |
| 3717 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3718 | return status; |
| 3719 | } |
| 3720 | |
| 3721 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3722 | |
| 3723 | bool forceVolumeReeval = false; |
| 3724 | // FIXME: workaround for truncated touch sounds |
| 3725 | // to be removed when the problem is handled by system UI |
| 3726 | uint32_t delayMs = 0; |
| 3727 | if (strategy == mCommunnicationStrategy) { |
| 3728 | forceVolumeReeval = true; |
| 3729 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3730 | updateInputRouting(); |
| 3731 | } |
| 3732 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3733 | |
| 3734 | return NO_ERROR; |
| 3735 | } |
| 3736 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3737 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 3738 | device_role_t role, |
| 3739 | AudioDeviceTypeAddrVector &devices) { |
| 3740 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3741 | } |
| 3742 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3743 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 3744 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3745 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3746 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3747 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3748 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3749 | return BAD_VALUE; |
| 3750 | } |
| 3751 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3752 | ALOGW_IF(status != NO_ERROR, |
| 3753 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 3754 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3755 | |
| 3756 | return status; |
| 3757 | } |
| 3758 | |
| 3759 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 3760 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3761 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3762 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3763 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3764 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3765 | return BAD_VALUE; |
| 3766 | } |
| 3767 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3768 | ALOGW_IF(status != NO_ERROR, |
| 3769 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 3770 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3771 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3772 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3773 | return status; |
| 3774 | } |
| 3775 | |
| 3776 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 3777 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 3778 | { |
| 3779 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 3780 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3781 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3782 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3783 | return BAD_VALUE; |
| 3784 | } |
| 3785 | |
| 3786 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 3787 | audioSource, role, devices); |
| 3788 | ALOGW_IF(status != NO_ERROR, |
| 3789 | "Engine could not remove devices role (%d) for capture preset %d", role, audioSource); |
| 3790 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3791 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3792 | return status; |
| 3793 | } |
| 3794 | |
| 3795 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 3796 | device_role_t role) { |
| 3797 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 3798 | |
| 3799 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
| 3800 | ALOGW_IF(status != NO_ERROR, |
| 3801 | "Engine could not clear devices role (%d) for capture preset %d", role, audioSource); |
| 3802 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3803 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3804 | return status; |
| 3805 | } |
| 3806 | |
| 3807 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 3808 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 3809 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 3810 | } |
| 3811 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3812 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3813 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3814 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3815 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3816 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3817 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3818 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 3819 | if (status != NO_ERROR) { |
| 3820 | ALOGE("%s() could not set device affinity for userId %d", |
| 3821 | __FUNCTION__, userId); |
| 3822 | return status; |
| 3823 | } |
| 3824 | |
| 3825 | // reevaluate outputs for all devices |
| 3826 | checkForDeviceAndOutputChanges(); |
| 3827 | updateCallAndOutputRouting(); |
| 3828 | |
| 3829 | return NO_ERROR; |
| 3830 | } |
| 3831 | |
| 3832 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3833 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3834 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 3835 | if (status != NO_ERROR) { |
| 3836 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 3837 | __FUNCTION__, userId); |
| 3838 | return status; |
| 3839 | } |
| 3840 | |
| 3841 | // reevaluate outputs for all devices |
| 3842 | checkForDeviceAndOutputChanges(); |
| 3843 | updateCallAndOutputRouting(); |
| 3844 | |
| 3845 | return NO_ERROR; |
| 3846 | } |
| 3847 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3848 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3849 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3850 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 3851 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3852 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 3853 | std::string stateLiteral; |
| 3854 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3855 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3856 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 3857 | "communications", "media", "record", "dock", "system", |
| 3858 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 3859 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 3860 | 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] | 3861 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 3862 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 3863 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 3864 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3865 | dst->append(" (MANUAL: "); |
| 3866 | dumpManualSurroundFormats(dst); |
| 3867 | dst->append(")"); |
| 3868 | } |
| 3869 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3870 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3871 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 3872 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 3873 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3874 | 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] | 3875 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 3876 | dst->append("\n"); |
| 3877 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 3878 | dst->append("\n"); |
| 3879 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3880 | mHwModulesAll.dump(dst); |
| 3881 | mOutputs.dump(dst); |
| 3882 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 3883 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3884 | mAudioPatches.dump(dst); |
| 3885 | mPolicyMixes.dump(dst); |
| 3886 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3887 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3888 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 3889 | for (auto& policy : mAllowedCapturePolicies) { |
| 3890 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 3891 | } |
| 3892 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3893 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 3894 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3895 | } |
| 3896 | |
| 3897 | status_t AudioPolicyManager::dump(int fd) |
| 3898 | { |
| 3899 | String8 result; |
| 3900 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 3901 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3902 | return NO_ERROR; |
| 3903 | } |
| 3904 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3905 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 3906 | { |
| 3907 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 3908 | return NO_ERROR; |
| 3909 | } |
| 3910 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3911 | // This function checks for the parameters which can be offloaded. |
| 3912 | // This can be enhanced depending on the capability of the DSP and policy |
| 3913 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3914 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3915 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3916 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3917 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3918 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3919 | offloadInfo.format, |
| 3920 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 3921 | offloadInfo.has_video); |
| 3922 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3923 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3924 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | // See if there is a profile to support this. |
| 3928 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3929 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3930 | offloadInfo.sample_rate, |
| 3931 | offloadInfo.format, |
| 3932 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3933 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 3934 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 3935 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 3936 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 3937 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3938 | if (profile == nullptr) { |
| 3939 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 3940 | } |
| 3941 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 3942 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 3943 | } |
| 3944 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3945 | } |
| 3946 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3947 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 3948 | const audio_attributes_t& attributes) { |
| 3949 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 3950 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 3951 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 3952 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3953 | config.sample_rate, |
| 3954 | config.format, |
| 3955 | config.channel_mask, |
| 3956 | output_flags, |
| 3957 | true /* directOnly */); |
| 3958 | ALOGV("%s() profile %sfound with name: %s, " |
| 3959 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 3960 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3961 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3962 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 3963 | |
| 3964 | // also try the MSD module if compatible profile not found |
| 3965 | if (profile == nullptr) { |
| 3966 | profile = getMsdProfileForOutput(outputDevices, |
| 3967 | config.sample_rate, |
| 3968 | config.format, |
| 3969 | config.channel_mask, |
| 3970 | output_flags, |
| 3971 | true /* directOnly */); |
| 3972 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 3973 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 3974 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 3975 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 3976 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 3977 | } |
| 3978 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3979 | } |
| 3980 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3981 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 3982 | bool durationIgnored) { |
| 3983 | if (mMasterMono) { |
| 3984 | return false; // no offloading if mono is set. |
| 3985 | } |
| 3986 | |
| 3987 | // Check if offload has been disabled |
| 3988 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 3989 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 3990 | return false; |
| 3991 | } |
| 3992 | |
| 3993 | // Check if stream type is music, then only allow offload as of now. |
| 3994 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 3995 | { |
| 3996 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 3997 | return false; |
| 3998 | } |
| 3999 | |
| 4000 | //TODO: enable audio offloading with video when ready |
| 4001 | const bool allowOffloadWithVideo = |
| 4002 | property_get_bool("audio.offload.video", false /* default_value */); |
| 4003 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 4004 | ALOGV("%s: has_video == true, returning false", __func__); |
| 4005 | return false; |
| 4006 | } |
| 4007 | |
| 4008 | //If duration is less than minimum value defined in property, return false |
| 4009 | const int min_duration_secs = property_get_int32( |
| 4010 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 4011 | if (!durationIgnored) { |
| 4012 | if (min_duration_secs >= 0) { |
| 4013 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 4014 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 4015 | __func__, min_duration_secs); |
| 4016 | return false; |
| 4017 | } |
| 4018 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 4019 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 4020 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 4021 | return false; |
| 4022 | } |
| 4023 | } |
| 4024 | |
| 4025 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 4026 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 4027 | // detects there is an active non offloadable effect. |
| 4028 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 4029 | // This may prevent offloading in rare situations where effects are left active by apps |
| 4030 | // in the background. |
| 4031 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 4032 | return false; |
| 4033 | } |
| 4034 | |
| 4035 | return true; |
| 4036 | } |
| 4037 | |
| 4038 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 4039 | const audio_config_t *config) { |
| 4040 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 4041 | offloadInfo.format = config->format; |
| 4042 | offloadInfo.sample_rate = config->sample_rate; |
| 4043 | offloadInfo.channel_mask = config->channel_mask; |
| 4044 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 4045 | offloadInfo.has_video = false; |
| 4046 | offloadInfo.is_streaming = false; |
| 4047 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 4048 | |
| 4049 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 4050 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4051 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 4052 | // only retain flags that will drive compressed offload or passthrough |
| 4053 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 4054 | if (offloadPossible) { |
| 4055 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 4056 | } |
| 4057 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 4058 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4059 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4060 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4061 | DeviceVector outputDevices = engineOutputDevices; |
| 4062 | // the MSD module checks for different conditions and output devices |
| 4063 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4064 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 4065 | continue; |
| 4066 | } |
| 4067 | outputDevices = getMsdAudioOutDevices(); |
| 4068 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4069 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | c8f7dfc | 2022-01-06 18:42:08 +0000 | [diff] [blame] | 4070 | if (!curProfile->isCompatibleProfile(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4071 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 4072 | config->format, nullptr /*updatedFormat*/, |
| 4073 | config->channel_mask, nullptr /*updatedChannelMask*/, |
| 4074 | flags)) { |
| 4075 | continue; |
| 4076 | } |
| 4077 | // reject profiles not corresponding to a device currently available |
| 4078 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 4079 | continue; |
| 4080 | } |
| 4081 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 4082 | != AUDIO_OUTPUT_FLAG_NONE) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 4083 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4084 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 4085 | // Already reports offload gapless supported. No need to report offload support. |
| 4086 | continue; |
| 4087 | } |
| 4088 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 4089 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 4090 | // If offload gapless is reported, no need to report offload support. |
| 4091 | directMode = (audio_direct_mode_t) ((directMode & |
| 4092 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 4093 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4094 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4095 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4096 | } |
| 4097 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame] | 4098 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | } |
| 4102 | return directMode; |
| 4103 | } |
| 4104 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4105 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4106 | AudioProfileVector& audioProfilesVector) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4107 | DeviceVector devices; |
| 4108 | status_t status = getDevicesForAttributes(*attr, devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4109 | if (status != OK) { |
| 4110 | return status; |
| 4111 | } |
| 4112 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4113 | if (devices.empty()) { |
| 4114 | return OK; // no output devices for the attributes |
| 4115 | } |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 4116 | return getProfilesForDevices(devices, audioProfilesVector, |
| 4117 | AUDIO_OUTPUT_FLAG_DIRECT /*flags*/, false /*isInput*/); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4118 | } |
| 4119 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4120 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 4121 | audio_port_type_t type, |
| 4122 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4123 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4124 | unsigned int *generation) |
| 4125 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4126 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 4127 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4128 | return BAD_VALUE; |
| 4129 | } |
| 4130 | 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] | 4131 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4132 | *num_ports = 0; |
| 4133 | } |
| 4134 | |
| 4135 | size_t portsWritten = 0; |
| 4136 | size_t portsMax = *num_ports; |
| 4137 | *num_ports = 0; |
| 4138 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4139 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 4140 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4141 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4142 | for (const auto& dev : mAvailableOutputDevices) { |
| 4143 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4144 | continue; |
| 4145 | } |
| 4146 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4147 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4148 | } |
| 4149 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4150 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4151 | } |
| 4152 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4153 | for (const auto& dev : mAvailableInputDevices) { |
| 4154 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4155 | continue; |
| 4156 | } |
| 4157 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4158 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4159 | } |
| 4160 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4161 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4162 | } |
| 4163 | } |
| 4164 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 4165 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 4166 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 4167 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4168 | } |
| 4169 | *num_ports += mInputs.size(); |
| 4170 | } |
| 4171 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4172 | size_t numOutputs = 0; |
| 4173 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4174 | if (!mOutputs[i]->isDuplicated()) { |
| 4175 | numOutputs++; |
| 4176 | if (portsWritten < portsMax) { |
| 4177 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4178 | } |
| 4179 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4180 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4181 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4182 | } |
| 4183 | } |
| 4184 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4185 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4186 | return NO_ERROR; |
| 4187 | } |
| 4188 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4189 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4190 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 4191 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 4192 | return BAD_VALUE; |
| 4193 | } |
| 4194 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 4195 | if (dev != 0) { |
| 4196 | dev->toAudioPort(port); |
| 4197 | return NO_ERROR; |
| 4198 | } |
| 4199 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 4200 | if (dev != 0) { |
| 4201 | dev->toAudioPort(port); |
| 4202 | return NO_ERROR; |
| 4203 | } |
| 4204 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 4205 | if (out != 0) { |
| 4206 | out->toAudioPort(port); |
| 4207 | return NO_ERROR; |
| 4208 | } |
| 4209 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 4210 | if (in != 0) { |
| 4211 | in->toAudioPort(port); |
| 4212 | return NO_ERROR; |
| 4213 | } |
| 4214 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4217 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 4218 | audio_patch_handle_t *handle, |
| 4219 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4220 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4221 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4222 | if (handle == NULL || patch == NULL) { |
| 4223 | return BAD_VALUE; |
| 4224 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4225 | 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] | 4226 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4227 | return BAD_VALUE; |
| 4228 | } |
| 4229 | // only one source per audio patch supported for now |
| 4230 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4231 | return INVALID_OPERATION; |
| 4232 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4233 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4234 | return INVALID_OPERATION; |
| 4235 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4236 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4237 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 4238 | return INVALID_OPERATION; |
| 4239 | } |
| 4240 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4241 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4242 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 4243 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 4244 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 4245 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 4246 | return BAD_VALUE; |
| 4247 | } |
| 4248 | ALOGV("%s between source %s and sink %s", __func__, |
| 4249 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 4250 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 4251 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 4252 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 4253 | const struct audio_port_config *source = &patch->sources[0]; |
| 4254 | sp<SourceClientDescriptor> sourceDesc = |
| 4255 | new InternalSourceClientDescriptor( |
| 4256 | portId, uid, attributes, *source, srcDevice, sinkDevice, |
| 4257 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes)); |
| 4258 | |
| 4259 | status_t status = |
| 4260 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 4261 | |
| 4262 | if (status != NO_ERROR) { |
| 4263 | return INVALID_OPERATION; |
| 4264 | } |
| 4265 | mAudioSources.add(portId, sourceDesc); |
| 4266 | return NO_ERROR; |
| 4267 | } |
| 4268 | |
| 4269 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 4270 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 4271 | const struct audio_patch *patch, |
| 4272 | audio_patch_handle_t &handle, |
| 4273 | uid_t uid, uint32_t delayMs) |
| 4274 | { |
| 4275 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 4276 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4277 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4278 | return INVALID_OPERATION; |
| 4279 | } |
| 4280 | sourceDesc->connect(handle, sinkDevice); |
| 4281 | if (isMsdPatch(handle)) { |
| 4282 | return NO_ERROR; |
| 4283 | } |
| 4284 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4285 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4286 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 4287 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 4288 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 4289 | goto FailurePatchAdded; |
| 4290 | } |
| 4291 | status = swOutput->start(); |
| 4292 | if (status != NO_ERROR) { |
| 4293 | goto FailureSourceAdded; |
| 4294 | } |
| 4295 | swOutput->addClient(sourceDesc); |
| 4296 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 4297 | if (status != NO_ERROR) { |
| 4298 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4299 | goto FailureSourceActive; |
| 4300 | } |
| 4301 | if (delayMs != 0) { |
| 4302 | usleep(delayMs * 1000); |
| 4303 | } |
| 4304 | return NO_ERROR; |
| 4305 | |
| 4306 | FailureSourceActive: |
| 4307 | swOutput->stop(); |
| 4308 | releaseOutput(sourceDesc->portId()); |
| 4309 | FailureSourceAdded: |
| 4310 | sourceDesc->setSwOutput(nullptr); |
| 4311 | FailurePatchAdded: |
| 4312 | releaseAudioPatchInternal(handle); |
| 4313 | return INVALID_OPERATION; |
| 4314 | } |
| 4315 | |
| 4316 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 4317 | audio_patch_handle_t *handle, |
| 4318 | uid_t uid, uint32_t delayMs, |
| 4319 | const sp<SourceClientDescriptor>& sourceDesc) |
| 4320 | { |
| 4321 | 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] | 4322 | sp<AudioPatch> patchDesc; |
| 4323 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 4324 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4325 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 4326 | patch->sources[0].role, |
| 4327 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4328 | #if LOG_NDEBUG == 0 |
| 4329 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4330 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 4331 | patch->sinks[i].role, |
| 4332 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4333 | } |
| 4334 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4335 | |
| 4336 | if (index >= 0) { |
| 4337 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4338 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 4339 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4340 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4341 | return INVALID_OPERATION; |
| 4342 | } |
| 4343 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4344 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4348 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4349 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4350 | 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] | 4351 | return BAD_VALUE; |
| 4352 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4353 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 4354 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4355 | if (patchDesc != 0) { |
| 4356 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4357 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 4358 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4359 | return BAD_VALUE; |
| 4360 | } |
| 4361 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4362 | DeviceVector devices; |
| 4363 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4364 | // Only support mix to devices connection |
| 4365 | // TODO add support for mix to mix connection |
| 4366 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4367 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4368 | return INVALID_OPERATION; |
| 4369 | } |
| 4370 | sp<DeviceDescriptor> devDesc = |
| 4371 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 4372 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4373 | 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] | 4374 | return BAD_VALUE; |
| 4375 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4376 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4377 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4378 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4379 | NULL, // updatedSamplingRate |
| 4380 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4381 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4382 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4383 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4384 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4385 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4386 | return INVALID_OPERATION; |
| 4387 | } |
| 4388 | devices.add(devDesc); |
| 4389 | } |
| 4390 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4391 | return INVALID_OPERATION; |
| 4392 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4393 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4394 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4395 | ALOGV("%s setting device %s on output %d", |
| 4396 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4397 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4398 | index = mAudioPatches.indexOfKey(*handle); |
| 4399 | if (index >= 0) { |
| 4400 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4401 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4402 | } |
| 4403 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4404 | patchDesc->setUid(uid); |
| 4405 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4406 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4407 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4408 | return INVALID_OPERATION; |
| 4409 | } |
| 4410 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4411 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 4412 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4413 | // only one sink supported when connecting an input device to a mix |
| 4414 | if (patch->num_sinks > 1) { |
| 4415 | return INVALID_OPERATION; |
| 4416 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4417 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4418 | if (inputDesc == NULL) { |
| 4419 | return BAD_VALUE; |
| 4420 | } |
| 4421 | if (patchDesc != 0) { |
| 4422 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 4423 | return BAD_VALUE; |
| 4424 | } |
| 4425 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4426 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4427 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4428 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4429 | return BAD_VALUE; |
| 4430 | } |
| 4431 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4432 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4433 | patch->sinks[0].sample_rate, |
| 4434 | NULL, /*updatedSampleRate*/ |
| 4435 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4436 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4437 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4438 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4439 | // FIXME for the parameter type, |
| 4440 | // and the NONE |
| 4441 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4442 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4443 | return INVALID_OPERATION; |
| 4444 | } |
| 4445 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4446 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4447 | device->toString().c_str(), inputDesc->mIoHandle); |
| 4448 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4449 | index = mAudioPatches.indexOfKey(*handle); |
| 4450 | if (index >= 0) { |
| 4451 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4452 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4453 | } |
| 4454 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4455 | patchDesc->setUid(uid); |
| 4456 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4457 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4458 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4459 | return INVALID_OPERATION; |
| 4460 | } |
| 4461 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4462 | // device to device connection |
| 4463 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4464 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4465 | return BAD_VALUE; |
| 4466 | } |
| 4467 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4468 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4469 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4470 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 4471 | return BAD_VALUE; |
| 4472 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4473 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4474 | //update source and sink with our own data as the data passed in the patch may |
| 4475 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4476 | PatchBuilder patchBuilder; |
| 4477 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4478 | |
| 4479 | // if first sink is to MSD, establish single MSD patch |
| 4480 | if (getMsdAudioOutDevices().contains( |
| 4481 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 4482 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 4483 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 4484 | goto installPatch; |
| 4485 | } |
| 4486 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4487 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 4488 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4489 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4490 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4491 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4492 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4493 | return INVALID_OPERATION; |
| 4494 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4495 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4496 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4497 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4498 | return BAD_VALUE; |
| 4499 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4500 | audio_port_config sinkPortConfig = {}; |
| 4501 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 4502 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4503 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4504 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 4505 | // volume management purpose (tracking activity) |
| 4506 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 4507 | // in config XML to reach the sink so that is can be declared as available. |
| 4508 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 4509 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4510 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4511 | // take care of dynamic routing for SwOutput selection, |
| 4512 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 4513 | audio_stream_type_t stream = sourceDesc->stream(); |
| 4514 | audio_attributes_t resultAttr; |
| 4515 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4516 | config.sample_rate = sourceDesc->config().sample_rate; |
François Gaffie | 2a24db4 | 2022-04-04 16:45:02 +0200 | [diff] [blame] | 4517 | audio_channel_mask_t sourceMask = sourceDesc->config().channel_mask; |
| 4518 | config.channel_mask = |
| 4519 | (audio_channel_mask_get_representation(sourceMask) |
| 4520 | == AUDIO_CHANNEL_REPRESENTATION_INDEX) ? sourceMask |
| 4521 | : audio_channel_mask_in_to_out(sourceMask); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4522 | config.format = sourceDesc->config().format; |
| 4523 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4524 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 4525 | bool isRequestedDeviceForExclusiveUse = false; |
| 4526 | output_type_t outputType; |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4527 | bool isSpatialized; |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4528 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 4529 | &stream, sourceDesc->uid(), &config, &flags, |
| 4530 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
Eric Laurent | b0a7bc9 | 2022-04-05 15:06:08 +0200 | [diff] [blame] | 4531 | nullptr, &outputType, &isSpatialized); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4532 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4533 | ALOGV("%s no output for device %s", |
| 4534 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 4535 | return INVALID_OPERATION; |
| 4536 | } |
| 4537 | outputDesc = mOutputs.valueFor(output); |
| 4538 | if (outputDesc->isDuplicated()) { |
| 4539 | ALOGE("%s output is duplicated", __func__); |
| 4540 | return INVALID_OPERATION; |
| 4541 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame^] | 4542 | bool closeOutput = outputDesc->mDirectOpenCount != 0; |
| 4543 | sourceDesc->setSwOutput(outputDesc, closeOutput); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4544 | } else { |
| 4545 | // Same for "raw patches" aka created from createAudioPatch API |
| 4546 | SortedVector<audio_io_handle_t> outputs = |
| 4547 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 4548 | // if the sink device is reachable via an opened output stream, request to |
| 4549 | // go via this output stream by adding a second source to the patch |
| 4550 | // description |
| 4551 | output = selectOutput(outputs); |
| 4552 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4553 | ALOGE("%s no output available for internal patch sink", __func__); |
| 4554 | return INVALID_OPERATION; |
| 4555 | } |
| 4556 | outputDesc = mOutputs.valueFor(output); |
| 4557 | if (outputDesc->isDuplicated()) { |
| 4558 | ALOGV("%s output for device %s is duplicated", |
| 4559 | __func__, sinkDevice->toString().c_str()); |
| 4560 | return INVALID_OPERATION; |
| 4561 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame^] | 4562 | sourceDesc->setSwOutput(outputDesc, /* closeOutput= */ false); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4563 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4564 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 4565 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4566 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 4567 | // - 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] | 4568 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 4569 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4570 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 4571 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4572 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4573 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4574 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4575 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4576 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4577 | return INVALID_OPERATION; |
| 4578 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4579 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4580 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4581 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 4582 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4583 | // for volume control, we may need a valid stream |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4584 | srcMixPortConfig.ext.mix.usecase.stream = !sourceDesc->isInternal() ? |
| 4585 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 4586 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4587 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4588 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4589 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4590 | } |
| 4591 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 4592 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4593 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4594 | status_t status = installPatch( |
| 4595 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 4596 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4597 | 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] | 4598 | return INVALID_OPERATION; |
| 4599 | } |
| 4600 | } else { |
| 4601 | return BAD_VALUE; |
| 4602 | } |
| 4603 | } else { |
| 4604 | return BAD_VALUE; |
| 4605 | } |
| 4606 | return NO_ERROR; |
| 4607 | } |
| 4608 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4609 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4610 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4611 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4612 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 4613 | |
| 4614 | if (index < 0) { |
| 4615 | return BAD_VALUE; |
| 4616 | } |
| 4617 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4618 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 4619 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4620 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4621 | return INVALID_OPERATION; |
| 4622 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4623 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 4624 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 4625 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 4626 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 4627 | portId = sourceDesc->portId(); |
| 4628 | break; |
| 4629 | } |
| 4630 | } |
| 4631 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 4632 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4633 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4634 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4635 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4636 | uint32_t delayMs, |
| 4637 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4638 | { |
| 4639 | ALOGV("%s patch %d", __func__, handle); |
| 4640 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 4641 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 4642 | return BAD_VALUE; |
| 4643 | } |
| 4644 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4645 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4646 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4647 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4648 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4649 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4650 | 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] | 4651 | return BAD_VALUE; |
| 4652 | } |
| 4653 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4654 | setOutputDevices(outputDesc, |
| 4655 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 4656 | true, |
| 4657 | 0, |
| 4658 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4659 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4660 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4661 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4662 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4663 | 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] | 4664 | return BAD_VALUE; |
| 4665 | } |
| 4666 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4667 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4668 | true, |
| 4669 | NULL); |
| 4670 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4671 | status_t status = |
| 4672 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 4673 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 4674 | __func__, status, patchDesc->getAfHandle()); |
| 4675 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4676 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4677 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4678 | // SW or HW Bridge |
| 4679 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 4680 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4681 | 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] | 4682 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 4683 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 4684 | outputDesc = sourceDesc->swOutput().promote(); |
| 4685 | } |
| 4686 | if (outputDesc == nullptr) { |
| 4687 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 4688 | // releaseOutput has already called closeOutput in case of direct output |
| 4689 | return NO_ERROR; |
| 4690 | } |
François Gaffie | 7e39df2 | 2022-04-26 12:48:49 +0200 | [diff] [blame^] | 4691 | patchHandle = outputDesc->getPatchHandle(); |
| 4692 | // When a Sw bridge is released, the mixer used by this bridge will release its |
| 4693 | // patch at AudioFlinger side. Hence, the mixer audio patch must be recreated |
| 4694 | // Reuse patch handle to force audio flinger removing initial mixer patch removal |
| 4695 | // updating hal patch handle (prevent leaks). |
| 4696 | // While using a HwBridge, force reconsidering device only if not reusing an existing |
| 4697 | // output and no more activity on output (will force to close). |
| 4698 | bool force = sourceDesc->useSwBridge() || |
| 4699 | (sourceDesc->canCloseOutput() && !outputDesc->isActive()); |
| 4700 | // APM pattern is to have always outputs opened / patch realized for reachable devices. |
| 4701 | // Update device may result to NONE (empty), coupled with force, it releases the patch. |
| 4702 | // Reconsider device only for cases: |
| 4703 | // 1 / Active Output |
| 4704 | // 2 / Inactive Output previously hosting HwBridge |
| 4705 | // 3 / Inactive Output previously hosting SwBridge that can be closed. |
| 4706 | bool updateDevice = outputDesc->isActive() || !sourceDesc->useSwBridge() || |
| 4707 | sourceDesc->canCloseOutput(); |
| 4708 | setOutputDevices(outputDesc, |
| 4709 | updateDevice ? getNewOutputDevices(outputDesc, true /*fromCache*/) : |
| 4710 | outputDesc->devices(), |
| 4711 | force, |
| 4712 | 0, |
| 4713 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4714 | } else { |
| 4715 | return BAD_VALUE; |
| 4716 | } |
| 4717 | } else { |
| 4718 | return BAD_VALUE; |
| 4719 | } |
| 4720 | return NO_ERROR; |
| 4721 | } |
| 4722 | |
| 4723 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 4724 | struct audio_patch *patches, |
| 4725 | unsigned int *generation) |
| 4726 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4727 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4728 | return BAD_VALUE; |
| 4729 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4730 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4731 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4732 | } |
| 4733 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4734 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4735 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4736 | ALOGV("setAudioPortConfig()"); |
| 4737 | |
| 4738 | if (config == NULL) { |
| 4739 | return BAD_VALUE; |
| 4740 | } |
| 4741 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 4742 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4743 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 4744 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4745 | } |
| 4746 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4747 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4748 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 4749 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4750 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4751 | if (outputDesc == NULL) { |
| 4752 | return BAD_VALUE; |
| 4753 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4754 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 4755 | "setAudioPortConfig() called on duplicated output %d", |
| 4756 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4757 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4758 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4759 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4760 | if (inputDesc == NULL) { |
| 4761 | return BAD_VALUE; |
| 4762 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4763 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4764 | } else { |
| 4765 | return BAD_VALUE; |
| 4766 | } |
| 4767 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 4768 | sp<DeviceDescriptor> deviceDesc; |
| 4769 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 4770 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 4771 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 4772 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 4773 | } else { |
| 4774 | return BAD_VALUE; |
| 4775 | } |
| 4776 | if (deviceDesc == NULL) { |
| 4777 | return BAD_VALUE; |
| 4778 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4779 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4780 | } else { |
| 4781 | return BAD_VALUE; |
| 4782 | } |
| 4783 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4784 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4785 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 4786 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4787 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4788 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 4789 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4790 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4791 | if (status != NO_ERROR) { |
| 4792 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4793 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4794 | |
| 4795 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4796 | } |
| 4797 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4798 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 4799 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4800 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4801 | clearAudioPatches(uid); |
| 4802 | clearSessionRoutes(uid); |
| 4803 | } |
| 4804 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4805 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 4806 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4807 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4808 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4809 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4810 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4811 | } |
| 4812 | } |
| 4813 | } |
| 4814 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4815 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4816 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4817 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 4818 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 4819 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 4820 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4821 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4822 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 4823 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 4824 | continue; |
| 4825 | } |
| 4826 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4827 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4828 | continue; |
| 4829 | } |
| 4830 | // If the default device for this strategy is on another output mix, |
| 4831 | // invalidate all tracks in this strategy to force re connection. |
| 4832 | // Otherwise select new device on the output mix. |
| 4833 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4834 | for (auto stream : mEngine->getStreamTypesForProductStrategy(ps)) { |
| 4835 | mpClientInterface->invalidateStream(stream); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4836 | } |
| 4837 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4838 | setOutputDevices( |
| 4839 | outputDesc, getNewOutputDevices(outputDesc, false /*fromCache*/), false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4840 | } |
| 4841 | } |
| 4842 | } |
| 4843 | |
| 4844 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 4845 | { |
| 4846 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4847 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4848 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4849 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4850 | for (const auto& client : outputDesc->getClientIterable()) { |
| 4851 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 4852 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4853 | auto clientStrategy = client->strategy(); |
| 4854 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 4855 | end(affectedStrategies)) { |
| 4856 | continue; |
| 4857 | } |
| 4858 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } |
| 4861 | } |
| 4862 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4863 | for (const auto& strategy : affectedStrategies) { |
| 4864 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | // remove input routes associated with this uid |
| 4868 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4869 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4870 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4871 | for (const auto& client : inputDesc->getClientIterable()) { |
| 4872 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 4873 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 4874 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4875 | } |
| 4876 | } |
| 4877 | } |
| 4878 | // reroute inputs if necessary |
| 4879 | SortedVector<audio_io_handle_t> inputsToClose; |
| 4880 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4881 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 4882 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4883 | inputsToClose.add(inputDesc->mIoHandle); |
| 4884 | } |
| 4885 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4886 | for (const auto& input : inputsToClose) { |
| 4887 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4891 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 4892 | { |
| 4893 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4894 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 4895 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4896 | stopAudioSource(mAudioSources.keyAt(i)); |
| 4897 | } |
| 4898 | } |
| 4899 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4900 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4901 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 4902 | audio_io_handle_t *ioHandle, |
| 4903 | audio_devices_t *device) |
| 4904 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 4905 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 4906 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4907 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4908 | *device = mEngine->getInputDeviceForAttributes(attr)->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4909 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 4910 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4911 | } |
| 4912 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4913 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4914 | const audio_attributes_t *attributes, |
| 4915 | audio_port_handle_t *portId, |
| 4916 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4917 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4918 | ALOGV("%s", __FUNCTION__); |
| 4919 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 4920 | |
| 4921 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 4922 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 4923 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4924 | return BAD_VALUE; |
| 4925 | } |
| 4926 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4927 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 4928 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4929 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 4930 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4931 | return INVALID_OPERATION; |
| 4932 | } |
| 4933 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4934 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4935 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4936 | String8(source->ext.device.address), |
| 4937 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4938 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4939 | 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] | 4940 | return BAD_VALUE; |
| 4941 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4942 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 4943 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4944 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4945 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4946 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4947 | mEngine->getStreamTypeForAttributes(*attributes), |
| 4948 | mEngine->getProductStrategyForAttributes(*attributes), |
| 4949 | toVolumeSource(*attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4950 | |
| 4951 | status_t status = connectAudioSource(sourceDesc); |
| 4952 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4953 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4954 | } |
| 4955 | return status; |
| 4956 | } |
| 4957 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 4958 | sp<SourceClientDescriptor> AudioPolicyManager::startAudioSourceInternal( |
| 4959 | const struct audio_port_config *source, const audio_attributes_t *attributes, uid_t uid) |
| 4960 | { |
| 4961 | ALOGV("%s", __FUNCTION__); |
| 4962 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 4963 | |
| 4964 | status_t status = startAudioSource(source, attributes, &portId, uid); |
| 4965 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 4966 | return mAudioSources.valueFor(portId); |
| 4967 | } |
| 4968 | |
| 4969 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4970 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4971 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4972 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4973 | |
| 4974 | // make sure we only have one patch per source. |
| 4975 | disconnectAudioSource(sourceDesc); |
| 4976 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4977 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4978 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 4979 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 4980 | sourceDesc->srcDevice()->type(), |
| 4981 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 4982 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4983 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 4984 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4985 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4986 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4987 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 4988 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 4989 | return INVALID_OPERATION; |
| 4990 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4991 | PatchBuilder patchBuilder; |
| 4992 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 4993 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4994 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4995 | return connectAudioSourceToSink( |
| 4996 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, 0 /*delayMs*/); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4997 | } |
| 4998 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4999 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5000 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5001 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 5002 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5003 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5004 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5005 | return BAD_VALUE; |
| 5006 | } |
| 5007 | status_t status = disconnectAudioSource(sourceDesc); |
| 5008 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5009 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5010 | return status; |
| 5011 | } |
| 5012 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5013 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 5014 | { |
| 5015 | if (mMasterMono == mono) { |
| 5016 | return NO_ERROR; |
| 5017 | } |
| 5018 | mMasterMono = mono; |
| 5019 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 5020 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 5021 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 5022 | // |
| 5023 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 5024 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 5025 | // play back offloaded. |
| 5026 | if (mMasterMono) { |
| 5027 | Vector<audio_io_handle_t> offloaded; |
| 5028 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5029 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5030 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 5031 | offloaded.push(desc->mIoHandle); |
| 5032 | } |
| 5033 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5034 | for (const auto& handle : offloaded) { |
| 5035 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5036 | } |
| 5037 | } |
| 5038 | // update master mono for all remaining outputs |
| 5039 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 5040 | updateMono(mOutputs.keyAt(i)); |
| 5041 | } |
| 5042 | return NO_ERROR; |
| 5043 | } |
| 5044 | |
| 5045 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 5046 | { |
| 5047 | *mono = mMasterMono; |
| 5048 | return NO_ERROR; |
| 5049 | } |
| 5050 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5051 | float AudioPolicyManager::getStreamVolumeDB( |
| 5052 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 5053 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5054 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 5055 | } |
| 5056 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5057 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 5058 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5059 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5060 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5061 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 5062 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5063 | return BAD_VALUE; |
| 5064 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5065 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 5066 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5067 | |
| 5068 | size_t formatsWritten = 0; |
| 5069 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 5070 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5071 | *numSurroundFormats = mConfig.getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5072 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5073 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5074 | for (const auto& format: mConfig.getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5075 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5076 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5077 | bool formatEnabled = true; |
| 5078 | switch (forceUse) { |
| 5079 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5080 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5081 | break; |
| 5082 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 5083 | formatEnabled = false; |
| 5084 | break; |
| 5085 | default: // AUTO or ALWAYS => true |
| 5086 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5087 | } |
| 5088 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 5089 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5090 | } |
| 5091 | return NO_ERROR; |
| 5092 | } |
| 5093 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5094 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 5095 | audio_format_t *surroundFormats) { |
| 5096 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 5097 | return BAD_VALUE; |
| 5098 | } |
| 5099 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 5100 | __func__, *numSurroundFormats, surroundFormats); |
| 5101 | |
| 5102 | size_t formatsWritten = 0; |
| 5103 | size_t formatsMax = *numSurroundFormats; |
| 5104 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 5105 | |
| 5106 | // Return formats from all device profiles that have already been resolved by |
| 5107 | // checkOutputsForDevice(). |
| 5108 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 5109 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 5110 | audio_devices_t deviceType = device->type(); |
| 5111 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 5112 | // returns formats reported by HDMI devices. |
| 5113 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 5114 | continue; |
| 5115 | } |
| 5116 | // Formats reported by sink devices |
| 5117 | std::unordered_set<audio_format_t> formatset; |
| 5118 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 5119 | formatset.insert(it->second.begin(), it->second.end()); |
| 5120 | } |
| 5121 | |
| 5122 | // Formats hard-coded in the in policy configuration file (if any). |
| 5123 | FormatVector encodedFormats = device->encodedFormats(); |
| 5124 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 5125 | // Filter the formats which are supported by the vendor hardware. |
| 5126 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
| 5127 | if (mConfig.getSurroundFormats().count(*it) != 0) { |
| 5128 | formats.insert(*it); |
| 5129 | } else { |
| 5130 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5131 | if (pair.second.count(*it) != 0) { |
| 5132 | formats.insert(pair.first); |
| 5133 | break; |
| 5134 | } |
| 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | } |
| 5139 | *numSurroundFormats = formats.size(); |
| 5140 | for (const auto& format: formats) { |
| 5141 | if (formatsWritten < formatsMax) { |
| 5142 | surroundFormats[formatsWritten++] = format; |
| 5143 | } |
| 5144 | } |
| 5145 | return NO_ERROR; |
| 5146 | } |
| 5147 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5148 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 5149 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5150 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5151 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 5152 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5153 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5154 | return BAD_VALUE; |
| 5155 | } |
| 5156 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5157 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 5158 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5159 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5160 | return INVALID_OPERATION; |
| 5161 | } |
| 5162 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5163 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5164 | return NO_ERROR; |
| 5165 | } |
| 5166 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5167 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5168 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5169 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5170 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5171 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5172 | } |
| 5173 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5174 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5175 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5176 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5177 | } |
| 5178 | } |
| 5179 | |
| 5180 | sp<SwAudioOutputDescriptor> outputDesc; |
| 5181 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5182 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 5183 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5184 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 5185 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5186 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5187 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5188 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5189 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5190 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5191 | name.c_str(), |
| 5192 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5193 | if (status != NO_ERROR) { |
| 5194 | continue; |
| 5195 | } |
| 5196 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5197 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5198 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5199 | name.c_str(), |
| 5200 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5201 | profileUpdated |= (status == NO_ERROR); |
| 5202 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5203 | // 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] | 5204 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5205 | AUDIO_DEVICE_IN_HDMI); |
| 5206 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 5207 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5208 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5209 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5210 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5211 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5212 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5213 | name.c_str(), |
| 5214 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5215 | if (status != NO_ERROR) { |
| 5216 | continue; |
| 5217 | } |
| 5218 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5219 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5220 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5221 | name.c_str(), |
| 5222 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5223 | profileUpdated |= (status == NO_ERROR); |
| 5224 | } |
| 5225 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5226 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5227 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5228 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 5232 | } |
| 5233 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5234 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5235 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5236 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 5237 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5238 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5239 | } |
| 5240 | } |
| 5241 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 5242 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 5243 | { |
| 5244 | for (const auto& hwModule : mHwModules) { |
| 5245 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5246 | for (const auto &outProfile : outputProfiles) { |
| 5247 | struct audio_port audioPort; |
| 5248 | outProfile->toAudioPort(&audioPort); |
| 5249 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 5250 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5251 | return true; |
| 5252 | } |
| 5253 | } |
| 5254 | } |
| 5255 | } |
| 5256 | return false; |
| 5257 | } |
| 5258 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 5259 | bool AudioPolicyManager::isUltrasoundSupported() |
| 5260 | { |
| 5261 | bool hasUltrasoundOutput = false; |
| 5262 | bool hasUltrasoundInput = false; |
| 5263 | for (const auto& hwModule : mHwModules) { |
| 5264 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5265 | if (!hasUltrasoundOutput) { |
| 5266 | for (const auto &outProfile : outputProfiles) { |
| 5267 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 5268 | hasUltrasoundOutput = true; |
| 5269 | break; |
| 5270 | } |
| 5271 | } |
| 5272 | } |
| 5273 | |
| 5274 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5275 | if (!hasUltrasoundInput) { |
| 5276 | for (const auto &inputProfile : inputProfiles) { |
| 5277 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 5278 | hasUltrasoundInput = true; |
| 5279 | break; |
| 5280 | } |
| 5281 | } |
| 5282 | } |
| 5283 | |
| 5284 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 5285 | return true; |
| 5286 | } |
| 5287 | return false; |
| 5288 | } |
| 5289 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5290 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 5291 | { |
| 5292 | return getConfig().isCallScreenModeSupported(); |
| 5293 | } |
| 5294 | |
| 5295 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5296 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5297 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5298 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5299 | if (!sourceDesc->isConnected()) { |
| 5300 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 5301 | return NO_ERROR; |
| 5302 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5303 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5304 | if (swOutput != 0) { |
| 5305 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5306 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5307 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5308 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5309 | if (releaseOutput(sourceDesc->portId())) { |
| 5310 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 5311 | // no need to release audio patch here but just return NO_ERROR. |
| 5312 | return NO_ERROR; |
| 5313 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5314 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5315 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5316 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5317 | // close Hwoutput and remove from mHwOutputs |
| 5318 | } else { |
| 5319 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 5320 | } |
| 5321 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5322 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5323 | sourceDesc->disconnect(); |
| 5324 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5325 | } |
| 5326 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5327 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 5328 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5329 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5330 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5331 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5332 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5333 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5334 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 5335 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5336 | source = sourceDesc; |
| 5337 | break; |
| 5338 | } |
| 5339 | } |
| 5340 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5341 | } |
| 5342 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5343 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5344 | const audio_config_t *config, |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5345 | const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5346 | { |
| 5347 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 5348 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5349 | // 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] | 5350 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5351 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 5352 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 5353 | return false; |
| 5354 | } |
| 5355 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 5356 | return false; |
| 5357 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5358 | } |
| 5359 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5360 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5361 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5362 | if (profile == nullptr) { |
| 5363 | return false; |
| 5364 | } |
| 5365 | |
| 5366 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 5367 | // the AUDIO_CONFIG_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5368 | // 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] | 5369 | // some positional channel masks. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5370 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5371 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5372 | if (!audio_is_channel_mask_spatialized(config->channel_mask)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5373 | return false; |
| 5374 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5375 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5376 | return true; |
| 5377 | } |
| 5378 | |
| 5379 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 5380 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 5381 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5382 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 5383 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5384 | audio_attributes_t attr = client->attributes(); |
| 5385 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 5386 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5387 | audio_config_base_t clientConfig = client->config(); |
| 5388 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5389 | if (desc != mSpatializerOutput |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5390 | && canBeSpatializedInt(&attr, &config, devicesTypeAddress)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5391 | streamsToInvalidate.insert(client->stream()); |
| 5392 | } |
| 5393 | } |
| 5394 | } |
| 5395 | |
| 5396 | for (audio_stream_type_t stream : streamsToInvalidate) { |
| 5397 | mpClientInterface->invalidateStream(stream); |
| 5398 | } |
| 5399 | } |
| 5400 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5401 | |
| 5402 | bool AudioPolicyManager::isOutputOnlyAvailableRouteToSomeDevice( |
| 5403 | const sp<SwAudioOutputDescriptor>& outputDesc) { |
| 5404 | if (outputDesc->isDuplicated()) { |
| 5405 | return false; |
| 5406 | } |
| 5407 | DeviceVector devices = outputDesc->supportedDevices(); |
| 5408 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5409 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5410 | if (desc == outputDesc || desc->isDuplicated()) { |
| 5411 | continue; |
| 5412 | } |
| 5413 | DeviceVector sharedDevices = desc->filterSupportedDevices(devices); |
| 5414 | if (!sharedDevices.isEmpty() |
| 5415 | && (desc->devicesSupportEncodedFormats(sharedDevices.types()) |
| 5416 | == outputDesc->devicesSupportEncodedFormats(sharedDevices.types()))) { |
| 5417 | return false; |
| 5418 | } |
| 5419 | } |
| 5420 | return true; |
| 5421 | } |
| 5422 | |
| 5423 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5424 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5425 | const audio_attributes_t *attr, |
| 5426 | audio_io_handle_t *output) { |
| 5427 | *output = AUDIO_IO_HANDLE_NONE; |
| 5428 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5429 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 5430 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5431 | audio_config_t *configPtr = nullptr; |
| 5432 | audio_config_t config; |
| 5433 | if (mixerConfig != nullptr) { |
| 5434 | config = audio_config_initializer(mixerConfig); |
| 5435 | configPtr = &config; |
| 5436 | } |
Andy Hung | 9dd1a5b | 2022-05-10 15:39:39 -0700 | [diff] [blame] | 5437 | if (!canBeSpatializedInt(attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5438 | ALOGV("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5439 | return BAD_VALUE; |
| 5440 | } |
| 5441 | |
| 5442 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5443 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5444 | if (profile == nullptr) { |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5445 | 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] | 5446 | return BAD_VALUE; |
| 5447 | } |
| 5448 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5449 | std::vector<sp<SwAudioOutputDescriptor>> spatializerOutputs; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5450 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5451 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5452 | if (!desc->isDuplicated() |
| 5453 | && (desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 5454 | spatializerOutputs.push_back(desc); |
| 5455 | ALOGV("%s adding opened spatializer Output %d", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5456 | } |
| 5457 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5458 | mSpatializerOutput.clear(); |
| 5459 | bool outputsChanged = false; |
| 5460 | for (const auto& desc : spatializerOutputs) { |
| 5461 | if (desc->mProfile == profile |
| 5462 | && (configPtr == nullptr |
| 5463 | || configPtr->channel_mask == desc->mMixerChannelMask)) { |
| 5464 | mSpatializerOutput = desc; |
| 5465 | ALOGV("%s reusing current spatializer output %d", __func__, desc->mIoHandle); |
| 5466 | } else { |
| 5467 | ALOGV("%s closing spatializerOutput output %d to match channel mask %#x" |
| 5468 | " and devices %s", __func__, desc->mIoHandle, |
| 5469 | configPtr != nullptr ? configPtr->channel_mask : 0, |
| 5470 | devices.toString().c_str()); |
| 5471 | closeOutput(desc->mIoHandle); |
| 5472 | outputsChanged = true; |
| 5473 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5474 | } |
| 5475 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5476 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5477 | sp<SwAudioOutputDescriptor> desc = |
| 5478 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5479 | if (desc != nullptr) { |
| 5480 | mSpatializerOutput = desc; |
| 5481 | outputsChanged = true; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5482 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | checkVirtualizerClientRoutes(); |
| 5486 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5487 | if (outputsChanged) { |
| 5488 | mPreviousOutputs = mOutputs; |
| 5489 | mpClientInterface->onAudioPortListUpdate(); |
| 5490 | } |
| 5491 | |
| 5492 | if (mSpatializerOutput == nullptr) { |
| 5493 | ALOGV("%s could not open spatializer output with requested config", __func__); |
| 5494 | return BAD_VALUE; |
| 5495 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5496 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5497 | ALOGV("%s returning new spatializer output %d", __func__, *output); |
| 5498 | return OK; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5499 | } |
| 5500 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5501 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 5502 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5503 | return INVALID_OPERATION; |
| 5504 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5505 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5506 | return BAD_VALUE; |
| 5507 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5508 | |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5509 | if (!isOutputOnlyAvailableRouteToSomeDevice(mSpatializerOutput)) { |
| 5510 | ALOGV("%s closing spatializer output %d", __func__, mSpatializerOutput->mIoHandle); |
| 5511 | closeOutput(mSpatializerOutput->mIoHandle); |
| 5512 | //from now on mSpatializerOutput is null |
| 5513 | checkVirtualizerClientRoutes(); |
| 5514 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5515 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5516 | return NO_ERROR; |
| 5517 | } |
| 5518 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5519 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5520 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5521 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5522 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 5523 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 5524 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5525 | } |
| 5526 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5527 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5528 | if (std::string audioPolicyXmlConfigFile = audio_get_audio_policy_config_file(); |
| 5529 | !audioPolicyXmlConfigFile.empty()) { |
| 5530 | status_t ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile.c_str(), &config); |
| 5531 | if (ret == NO_ERROR) { |
| 5532 | config.setSource(audioPolicyXmlConfigFile); |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 5533 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5534 | return ret; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 5535 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5536 | return BAD_VALUE; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5537 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5538 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5539 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 5540 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5541 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5542 | 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] | 5543 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5544 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5545 | mA2dpSuspended(false), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5546 | mConfig(mHwModulesAll, mOutputDevicesAll, mInputDevicesAll, mDefaultOutputDevice), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5547 | mAudioPortGeneration(1), |
| 5548 | mBeaconMuteRefCount(0), |
| 5549 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5550 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5551 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5552 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5553 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5554 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5555 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5556 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5557 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 5558 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 5559 | { |
| 5560 | loadConfig(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5561 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5562 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 5563 | void AudioPolicyManager::loadConfig() { |
| 5564 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5565 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5566 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5567 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5568 | } |
| 5569 | |
| 5570 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 5571 | { |
| 5572 | auto engLib = EngineLibrary::load( |
| 5573 | "libaudiopolicyengine" + getConfig().getEngineLibraryNameSuffix() + ".so"); |
| 5574 | if (!engLib) { |
| 5575 | ALOGE("%s: Failed to load the engine library", __FUNCTION__); |
| 5576 | return NO_INIT; |
| 5577 | } |
| 5578 | mEngine = engLib->createEngine(); |
| 5579 | if (mEngine == nullptr) { |
| 5580 | ALOGE("%s: Failed to instantiate the APM engine", __FUNCTION__); |
| 5581 | return NO_INIT; |
| 5582 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5583 | } |
| 5584 | mEngine->setObserver(this); |
| 5585 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5586 | if (status != NO_ERROR) { |
| 5587 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 5588 | return status; |
| 5589 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5590 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 5591 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 5592 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 5593 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 5594 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5595 | // after parsing the config, mOutputDevicesAll and mInputDevicesAll contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5596 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5597 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5598 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5599 | // make sure default device is reachable |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5600 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { |
| 5601 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", |
| 5602 | mDefaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5603 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5604 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5605 | // If microphones address is empty, set it according to device type |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 5606 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5607 | if (mAvailableInputDevices[i]->address().empty()) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5608 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5609 | mAvailableInputDevices[i]->setAddress(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5610 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5611 | mAvailableInputDevices[i]->setAddress(AUDIO_BACK_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5612 | } |
| 5613 | } |
| 5614 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5615 | |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5616 | 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] | 5617 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 5618 | // Silence ALOGV statements |
| 5619 | property_set("log.tag." LOG_TAG, "D"); |
| 5620 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5621 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5622 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5623 | } |
| 5624 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5625 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5626 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5627 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5628 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5629 | } |
| 5630 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5631 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5632 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5633 | mAvailableOutputDevices.clear(); |
| 5634 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5635 | mOutputs.clear(); |
| 5636 | mInputs.clear(); |
| 5637 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 5638 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5639 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5640 | } |
| 5641 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5642 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5643 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5644 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5645 | } |
| 5646 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5647 | // --- |
| 5648 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5649 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 5650 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5651 | DeviceVector newDevices; |
| 5652 | onNewAudioModulesAvailableInt(&newDevices); |
| 5653 | if (!newDevices.empty()) { |
| 5654 | nextAudioPortGeneration(); |
| 5655 | mpClientInterface->onAudioPortListUpdate(); |
| 5656 | } |
| 5657 | } |
| 5658 | |
| 5659 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 5660 | { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5661 | for (const auto& hwModule : mHwModulesAll) { |
| 5662 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 5663 | continue; |
| 5664 | } |
| 5665 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
| 5666 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 5667 | ALOGW("could not open HW module %s", hwModule->getName()); |
| 5668 | continue; |
| 5669 | } |
| 5670 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 5671 | // open all output streams needed to access attached devices. |
| 5672 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5673 | // This also validates mAvailableOutputDevices list |
| 5674 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 5675 | if (!outProfile->canOpenNewIo()) { |
| 5676 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 5677 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 5678 | continue; |
| 5679 | } |
| 5680 | if (!outProfile->hasSupportedDevices()) { |
| 5681 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 5682 | continue; |
| 5683 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 5684 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 5685 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5686 | mTtsOutputAvailable = true; |
| 5687 | } |
| 5688 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5689 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 5690 | DeviceVector availProfileDevices = supportedDevices.filter(mOutputDevicesAll); |
| 5691 | sp<DeviceDescriptor> supportedDevice = 0; |
| 5692 | if (supportedDevices.contains(mDefaultOutputDevice)) { |
| 5693 | supportedDevice = mDefaultOutputDevice; |
| 5694 | } else { |
| 5695 | // choose first device present in profile's SupportedDevices also part of |
| 5696 | // mAvailableOutputDevices. |
| 5697 | if (availProfileDevices.isEmpty()) { |
| 5698 | continue; |
| 5699 | } |
| 5700 | supportedDevice = availProfileDevices.itemAt(0); |
| 5701 | } |
| 5702 | if (!mOutputDevicesAll.contains(supportedDevice)) { |
| 5703 | continue; |
| 5704 | } |
| 5705 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 5706 | mpClientInterface); |
| 5707 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 5708 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 5709 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5710 | AUDIO_STREAM_DEFAULT, |
| 5711 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 5712 | if (status != NO_ERROR) { |
| 5713 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 5714 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 5715 | continue; |
| 5716 | } |
| 5717 | for (const auto &device : availProfileDevices) { |
| 5718 | // give a valid ID to an attached device once confirmed it is reachable |
| 5719 | if (!device->isAttached()) { |
| 5720 | device->attach(hwModule); |
| 5721 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 5722 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5723 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5724 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 5725 | } |
| 5726 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5727 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5728 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 5729 | mPrimaryOutput = outputDesc; |
| 5730 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5731 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5732 | outputDesc->close(); |
| 5733 | } else { |
| 5734 | addOutput(output, outputDesc); |
| 5735 | setOutputDevices(outputDesc, |
| 5736 | DeviceVector(supportedDevice), |
| 5737 | true, |
| 5738 | 0, |
| 5739 | NULL); |
| 5740 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5741 | } |
| 5742 | // open input streams needed to access attached devices to validate |
| 5743 | // mAvailableInputDevices list |
| 5744 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 5745 | if (!inProfile->canOpenNewIo()) { |
| 5746 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 5747 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 5748 | continue; |
| 5749 | } |
| 5750 | if (!inProfile->hasSupportedDevices()) { |
| 5751 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 5752 | continue; |
| 5753 | } |
| 5754 | // chose first device present in profile's SupportedDevices also part of |
| 5755 | // available input devices |
| 5756 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 5757 | DeviceVector availProfileDevices = supportedDevices.filter(mInputDevicesAll); |
| 5758 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 5759 | ALOGV("%s: Input device list is empty! for profile %s", |
| 5760 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5761 | continue; |
| 5762 | } |
| 5763 | sp<AudioInputDescriptor> inputDesc = |
| 5764 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 5765 | |
| 5766 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 5767 | status_t status = inputDesc->open(nullptr, |
| 5768 | availProfileDevices.itemAt(0), |
| 5769 | AUDIO_SOURCE_MIC, |
| 5770 | AUDIO_INPUT_FLAG_NONE, |
| 5771 | &input); |
| 5772 | if (status != NO_ERROR) { |
| 5773 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 5774 | availProfileDevices.toString().c_str(), |
| 5775 | hwModule->getName()); |
| 5776 | continue; |
| 5777 | } |
| 5778 | for (const auto &device : availProfileDevices) { |
| 5779 | // give a valid ID to an attached device once confirmed it is reachable |
| 5780 | if (!device->isAttached()) { |
| 5781 | device->attach(hwModule); |
| 5782 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 5783 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5784 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5785 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 5786 | } |
| 5787 | } |
| 5788 | inputDesc->close(); |
| 5789 | } |
| 5790 | } |
Eric Laurent | e191d1b | 2022-04-15 11:59:25 +0200 | [diff] [blame] | 5791 | |
| 5792 | // Check if spatializer outputs can be closed until used. |
| 5793 | // mOutputs vector never contains duplicated outputs at this point. |
| 5794 | std::vector<audio_io_handle_t> outputsClosed; |
| 5795 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5796 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5797 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0 |
| 5798 | && !isOutputOnlyAvailableRouteToSomeDevice(desc)) { |
| 5799 | outputsClosed.push_back(desc->mIoHandle); |
| 5800 | desc->close(); |
| 5801 | } |
| 5802 | } |
| 5803 | for (auto output : outputsClosed) { |
| 5804 | removeOutput(output); |
| 5805 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5808 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 5809 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5810 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5811 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5812 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5813 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5814 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5815 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5816 | } |
| 5817 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5818 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 5819 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5820 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 5821 | ALOGV("%s: removing primary output", __func__); |
| 5822 | mPrimaryOutput = nullptr; |
| 5823 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5824 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5825 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5826 | } |
| 5827 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5828 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 5829 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5830 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5831 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5832 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5833 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5834 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5835 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5836 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5837 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5838 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5839 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5840 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5841 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5842 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5843 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5844 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5845 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5846 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5847 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5848 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 5849 | // first call getAudioPort to get the supported attributes from the HAL |
| 5850 | struct audio_port_v7 port = {}; |
| 5851 | device->toAudioPort(&port); |
| 5852 | status_t status = mpClientInterface->getAudioPort(&port); |
| 5853 | if (status == NO_ERROR) { |
| 5854 | device->importAudioPort(port); |
| 5855 | } |
| 5856 | |
| 5857 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5858 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5859 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5860 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5861 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5862 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 5863 | mOutputs.keyAt(i), device->toString().c_str()); |
| 5864 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5865 | } |
| 5866 | } |
| 5867 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5868 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5869 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5870 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 5871 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5872 | if (profile->supportsDevice(device)) { |
| 5873 | profiles.add(profile); |
| 5874 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 5875 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5876 | } |
| 5877 | } |
| 5878 | } |
| 5879 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 5880 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5881 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5882 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5883 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5884 | return BAD_VALUE; |
| 5885 | } |
| 5886 | |
| 5887 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 5888 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 5889 | 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] | 5890 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5891 | |
| 5892 | // nothing to do if one output is already opened for this profile |
| 5893 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5894 | for (j = 0; j < outputs.size(); j++) { |
| 5895 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5896 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5897 | // matching profile: save the sample rates, format and channel masks supported |
| 5898 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5899 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5900 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5901 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5902 | break; |
| 5903 | } |
| 5904 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5905 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5906 | continue; |
| 5907 | } |
| 5908 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5909 | if (!profile->canOpenNewIo()) { |
| 5910 | ALOGW("Max Output number %u already opened for this profile %s", |
| 5911 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 5912 | continue; |
| 5913 | } |
| 5914 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 5915 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5916 | deviceType, address.string(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5917 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 5918 | 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] | 5919 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5920 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5921 | profiles.removeAt(profile_index); |
| 5922 | profile_index--; |
| 5923 | } else { |
| 5924 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5925 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5926 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5927 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 5928 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5929 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5930 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5931 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5932 | if (device_distinguishes_on_address(deviceType)) { |
| 5933 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 5934 | device->toString().c_str()); |
| 5935 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 5936 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5937 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5938 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 5939 | } |
| 5940 | } |
| 5941 | |
| 5942 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5943 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5944 | return BAD_VALUE; |
| 5945 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5946 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5947 | // check if one opened output is not needed any more after disconnecting one device |
| 5948 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5949 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5950 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5951 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5952 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 5953 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5954 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5955 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5956 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 5957 | mOutputs.keyAt(i)); |
| 5958 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5959 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5960 | } |
| 5961 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5962 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5963 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5964 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 5965 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5966 | if (!profile->supportsDevice(device)) { |
| 5967 | continue; |
| 5968 | } |
| 5969 | ALOGV("checkOutputsForDevice(): " |
| 5970 | "clearing direct output profile %zu on module %s", |
| 5971 | j, hwModule->getName()); |
| 5972 | profile->clearAudioProfiles(); |
| 5973 | if (!profile->hasDynamicAudioProfile()) { |
| 5974 | continue; |
| 5975 | } |
| 5976 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 5977 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 5978 | // the capabilities of the devices that is supported by the IOProfile. |
| 5979 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 5980 | if (supportedDevice == device || |
| 5981 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 5982 | continue; |
| 5983 | } |
| 5984 | struct audio_port_v7 port; |
| 5985 | supportedDevice->toAudioPort(&port); |
| 5986 | status_t status = mpClientInterface->getAudioPort(&port); |
| 5987 | if (status == NO_ERROR) { |
| 5988 | supportedDevice->importAudioPort(port); |
| 5989 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | } |
| 5993 | } |
| 5994 | return NO_ERROR; |
| 5995 | } |
| 5996 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5997 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5998 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5999 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6000 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6001 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6002 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6003 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6004 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 6005 | } |
| 6006 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6007 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6008 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6009 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6010 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6011 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6012 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6013 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6014 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6015 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6016 | if (profile->supportsDevice(device)) { |
| 6017 | profiles.add(profile); |
| 6018 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 6019 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6020 | } |
| 6021 | } |
| 6022 | } |
| 6023 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6024 | if (profiles.isEmpty()) { |
| 6025 | ALOGW("%s: No input profile available for device %s", |
| 6026 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6027 | return BAD_VALUE; |
| 6028 | } |
| 6029 | |
| 6030 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 6031 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 6032 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 6033 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6034 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6035 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6036 | // nothing to do if one input is already opened for this profile |
| 6037 | size_t input_index; |
| 6038 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 6039 | desc = mInputs.valueAt(input_index); |
| 6040 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6041 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6042 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6043 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6044 | break; |
| 6045 | } |
| 6046 | } |
| 6047 | if (input_index != mInputs.size()) { |
| 6048 | continue; |
| 6049 | } |
| 6050 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 6051 | if (!profile->canOpenNewIo()) { |
| 6052 | ALOGW("Max Input number %u already opened for this profile %s", |
| 6053 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 6054 | continue; |
| 6055 | } |
| 6056 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6057 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6058 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6059 | status_t status = desc->open(nullptr, |
| 6060 | device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6061 | AUDIO_SOURCE_MIC, |
| 6062 | AUDIO_INPUT_FLAG_NONE, |
| 6063 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6064 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6065 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 6066 | const String8& address = String8(device->address().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6067 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6068 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6069 | mpClientInterface->setParameters(input, String8(param)); |
| 6070 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6071 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6072 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6073 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6074 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6075 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6076 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6077 | } |
| 6078 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6079 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6080 | addInput(input, desc); |
| 6081 | } |
| 6082 | } // endif input != 0 |
| 6083 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6084 | if (input == AUDIO_IO_HANDLE_NONE) { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 6085 | ALOGW("%s could not open input for device %s", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6086 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6087 | profiles.removeAt(profile_index); |
| 6088 | profile_index--; |
| 6089 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6090 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6091 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6092 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6093 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 6094 | } |
| 6095 | } // end scan profiles |
| 6096 | |
| 6097 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6098 | 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] | 6099 | return BAD_VALUE; |
| 6100 | } |
| 6101 | } else { |
| 6102 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6103 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6104 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6105 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6106 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6107 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6108 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6109 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6110 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 6111 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6112 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6113 | } |
| 6114 | } |
| 6115 | } |
| 6116 | } // end disconnect |
| 6117 | |
| 6118 | return NO_ERROR; |
| 6119 | } |
| 6120 | |
| 6121 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6122 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6123 | { |
| 6124 | ALOGV("closeOutput(%d)", output); |
| 6125 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6126 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 6127 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6128 | ALOGW("closeOutput() unknown output %d", output); |
| 6129 | return; |
| 6130 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6131 | const bool closingOutputWasActive = closingOutput->isActive(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6132 | mPolicyMixes.closeOutput(closingOutput); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6133 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6134 | // look for duplicated outputs connected to the output being removed. |
| 6135 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6136 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 6137 | if (dupOutput->isDuplicated() && |
| 6138 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 6139 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 6140 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6141 | // As all active tracks on duplicated output will be deleted, |
| 6142 | // and as they were also referenced on the other output, the reference |
| 6143 | // count for their stream type must be adjusted accordingly on |
| 6144 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6145 | const bool wasActive = remainingOutput->isActive(); |
| 6146 | // Note: no-op on the closing output where all clients has already been set inactive |
| 6147 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6148 | // stop() will be a no op if the output is still active but is needed in case all |
| 6149 | // active streams refcounts where cleared above |
| 6150 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6151 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6152 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6153 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 6154 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 6155 | |
| 6156 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6157 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6158 | } |
| 6159 | } |
| 6160 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6161 | nextAudioPortGeneration(); |
| 6162 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6163 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6164 | if (index >= 0) { |
| 6165 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6166 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6167 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6168 | mAudioPatches.removeItemsAt(index); |
| 6169 | mpClientInterface->onAudioPatchListUpdate(); |
| 6170 | } |
| 6171 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6172 | if (closingOutputWasActive) { |
| 6173 | closingOutput->stop(); |
| 6174 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6175 | closingOutput->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6176 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6177 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6178 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6179 | if (closingOutput == mSpatializerOutput) { |
| 6180 | mSpatializerOutput.clear(); |
| 6181 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6182 | |
| 6183 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 6184 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6185 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6186 | bool directOutputOpen = false; |
| 6187 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6188 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 6189 | directOutputOpen = true; |
| 6190 | break; |
| 6191 | } |
| 6192 | } |
| 6193 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6194 | ALOGV("no direct outputs open, reset MSD patches"); |
| 6195 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 6196 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 6197 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 6198 | // devices to patch to. This may be complicated by the fact that devices may become |
| 6199 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6200 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6201 | } |
| 6202 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 6206 | { |
| 6207 | ALOGV("closeInput(%d)", input); |
| 6208 | |
| 6209 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 6210 | if (inputDesc == NULL) { |
| 6211 | ALOGW("closeInput() unknown input %d", input); |
| 6212 | return; |
| 6213 | } |
| 6214 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6215 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6216 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6217 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6218 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6219 | if (index >= 0) { |
| 6220 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6221 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6222 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6223 | mAudioPatches.removeItemsAt(index); |
| 6224 | mpClientInterface->onAudioPatchListUpdate(); |
| 6225 | } |
| 6226 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6227 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6228 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6229 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6230 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 6231 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6232 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 6233 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6234 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6235 | } |
| 6236 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6237 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 6238 | const DeviceVector &devices, |
| 6239 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6240 | { |
| 6241 | SortedVector<audio_io_handle_t> outputs; |
| 6242 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6243 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6244 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6245 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 6246 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6247 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6248 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6249 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6250 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6251 | outputs.add(openOutputs.keyAt(i)); |
| 6252 | } |
| 6253 | } |
| 6254 | return outputs; |
| 6255 | } |
| 6256 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6257 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 6258 | { |
| 6259 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 6260 | // output is suspended before any tracks are moved to it |
| 6261 | checkA2dpSuspend(); |
| 6262 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6263 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6264 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6265 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 6266 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6267 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 6268 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 6269 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 6270 | // unnecessary rerouting by caching and reusing the arguments to |
| 6271 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 6272 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6273 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6274 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 6275 | // an event that changed routing likely occurred, inform upper layers |
| 6276 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6277 | } |
| 6278 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6279 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 6280 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6281 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6282 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 6283 | mEngine->getProductStrategyForAttributes(rAttr); |
| 6284 | } |
| 6285 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6286 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 6287 | { |
| 6288 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6289 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6290 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6291 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6292 | && !isCallRxAudioSource(sourceDesc) && !sourceDesc->isInternal()) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6293 | connectAudioSource(sourceDesc); |
| 6294 | } |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 6299 | { |
| 6300 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6301 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6302 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 6303 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 6304 | disconnectAudioSource(sourceDesc); |
| 6305 | } |
| 6306 | } |
| 6307 | } |
| 6308 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6309 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 6310 | { |
| 6311 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 6312 | |
| 6313 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 6314 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 6315 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6316 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 6317 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6318 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6319 | uint32_t maxLatency = 0; |
| 6320 | bool invalidate = false; |
| 6321 | // take into account dynamic audio policies related changes: if a client is now associated |
| 6322 | // to a different policy mix than at creation time, invalidate corresponding stream |
| 6323 | for (size_t i = 0; i < mPreviousOutputs.size() && !invalidate; i++) { |
| 6324 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 6325 | if (desc->isDuplicated()) { |
| 6326 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6327 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6328 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 6329 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 6330 | continue; |
| 6331 | } |
| 6332 | sp<AudioPolicyMix> primaryMix; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6333 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
| 6334 | client->uid(), client->flags(), primaryMix, nullptr); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6335 | if (status != OK) { |
| 6336 | continue; |
| 6337 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 6338 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6339 | invalidate = true; |
| 6340 | if (desc->isStrategyActive(psId)) { |
| 6341 | maxLatency = desc->latency(); |
| 6342 | } |
| 6343 | break; |
| 6344 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6345 | } |
| 6346 | } |
| 6347 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6348 | if (srcOutputs != dstOutputs || invalidate) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6349 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 6350 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6351 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6352 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6353 | if (desc == nullptr) continue; |
| 6354 | |
| 6355 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6356 | maxLatency = desc->latency(); |
| 6357 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6358 | |
| 6359 | if (invalidate) continue; |
| 6360 | |
| 6361 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 6362 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6363 | // a client on a non direct outputs has necessarily a linear PCM format |
| 6364 | // so we can call selectOutput() safely |
| 6365 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 6366 | client->flags(), |
| 6367 | client->config().format, |
| 6368 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 6369 | client->config().sample_rate, |
| 6370 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6371 | if (newOutput != srcOut) { |
| 6372 | invalidate = true; |
| 6373 | break; |
| 6374 | } |
| 6375 | } else { |
| 6376 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 6377 | client->config().sample_rate, |
| 6378 | client->config().format, |
| 6379 | client->config().channel_mask, |
| 6380 | client->flags(), |
| 6381 | true /* directOnly */); |
| 6382 | if (profile != desc->mProfile) { |
| 6383 | invalidate = true; |
| 6384 | break; |
| 6385 | } |
| 6386 | } |
| 6387 | } |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6388 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6389 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6390 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6391 | "%s: strategy %d, moving from output %s to output %s", __func__, psId, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6392 | std::to_string(srcOutputs[0]).c_str(), |
| 6393 | std::to_string(dstOutputs[0]).c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6394 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6395 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6396 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6397 | if (desc == nullptr) continue; |
| 6398 | |
| 6399 | if (desc->isStrategyActive(psId)) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6400 | setStrategyMute(psId, true, desc); |
| 6401 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6402 | newDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6403 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6404 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6405 | if (source != nullptr && !isCallRxAudioSource(source) && !source->isInternal()) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6406 | connectAudioSource(source); |
| 6407 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6408 | } |
| 6409 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6410 | // Move effects associated to this stream from previous output to new output |
| 6411 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6412 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6413 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6414 | // Move tracks associated to this stream (and linked) from previous output to new output |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6415 | if (invalidate) { |
| 6416 | for (auto stream : mEngine->getStreamTypesForProductStrategy(psId)) { |
| 6417 | mpClientInterface->invalidateStream(stream); |
| 6418 | } |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 6419 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6420 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 6421 | if (desc == nullptr) continue; |
| 6422 | |
| 6423 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 6424 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6425 | } |
| 6426 | } |
| 6427 | } |
| 6428 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6429 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6430 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6431 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 6432 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 6433 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6434 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6435 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6436 | } |
| 6437 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6438 | void AudioPolicyManager::checkSecondaryOutputs() { |
| 6439 | std::set<audio_stream_type_t> streamsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6440 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6441 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6442 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 6443 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6444 | sp<AudioPolicyMix> primaryMix; |
| 6445 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Dean Wheatley | d082f47 | 2022-02-04 11:10:48 +1100 | [diff] [blame] | 6446 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->config(), |
| 6447 | client->uid(), client->flags(), primaryMix, &secondaryMixes); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6448 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 6449 | for (auto &secondaryMix : secondaryMixes) { |
| 6450 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 6451 | if (outputDesc != nullptr && |
| 6452 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 6453 | secondaryDescs.push_back(outputDesc); |
| 6454 | } |
| 6455 | } |
| 6456 | |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6457 | if (status != OK) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6458 | streamsToInvalidate.insert(client->stream()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6459 | } else if (!std::equal( |
| 6460 | client->getSecondaryOutputs().begin(), |
| 6461 | client->getSecondaryOutputs().end(), |
| 6462 | secondaryDescs.begin(), secondaryDescs.end())) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6463 | if (!audio_is_linear_pcm(client->config().format)) { |
| 6464 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 6465 | // behavior when the secondary output is changed. |
| 6466 | streamsToInvalidate.insert(client->stream()); |
| 6467 | } else { |
| 6468 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 6469 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 6470 | for (const auto &secondaryDesc: secondaryDescs) { |
| 6471 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 6472 | weakSecondaryDescs.push_back(secondaryDesc); |
| 6473 | } |
| 6474 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 6475 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6476 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6477 | } |
| 6478 | } |
| 6479 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6480 | if (!trackSecondaryOutputs.empty()) { |
| 6481 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 6482 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6483 | for (audio_stream_type_t stream : streamsToInvalidate) { |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6484 | 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] | 6485 | mpClientInterface->invalidateStream(stream); |
| 6486 | } |
| 6487 | } |
| 6488 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6489 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 6490 | AudioDeviceTypeAddrVector devices; |
| 6491 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 6492 | for (const auto &device : devices) { |
| 6493 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 6494 | return true; |
| 6495 | } |
| 6496 | } |
| 6497 | return false; |
| 6498 | } |
| 6499 | |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 6500 | bool AudioPolicyManager::isHearingAidUsedForComm() const { |
| 6501 | DeviceVector devices = mEngine->getOutputDevicesForStream(AUDIO_STREAM_VOICE_CALL, |
| 6502 | true /*fromCache*/); |
| 6503 | for (const auto &device : devices) { |
| 6504 | if (device->type() == AUDIO_DEVICE_OUT_HEARING_AID) { |
| 6505 | return true; |
| 6506 | } |
| 6507 | } |
| 6508 | return false; |
| 6509 | } |
| 6510 | |
| 6511 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6512 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6513 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6514 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 6515 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6516 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6517 | return; |
| 6518 | } |
| 6519 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6520 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6521 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 6522 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6523 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6524 | |
| 6525 | // if suspended, restore A2DP output if: |
| 6526 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6527 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6528 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6529 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6530 | // if not suspended, suspend A2DP output if: |
| 6531 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6532 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6533 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6534 | // |
| 6535 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6536 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6537 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6538 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6539 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6540 | |
| 6541 | mpClientInterface->restoreOutput(a2dpOutput); |
| 6542 | mA2dpSuspended = false; |
| 6543 | } |
| 6544 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6545 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6546 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6547 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6548 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6549 | |
| 6550 | mpClientInterface->suspendOutput(a2dpOutput); |
| 6551 | mA2dpSuspended = true; |
| 6552 | } |
| 6553 | } |
| 6554 | } |
| 6555 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6556 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 6557 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6558 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6559 | DeviceVector devices; |
| 6560 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 6561 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6562 | if (index >= 0) { |
| 6563 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6564 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6565 | ALOGV("%s device %s forced by patch %d", __func__, |
| 6566 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 6567 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6568 | } |
| 6569 | } |
| 6570 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 6571 | // Do not retrieve engine device for outputs through MSD |
| 6572 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 6573 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 6574 | return outputDesc->devices(); |
| 6575 | } |
| 6576 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6577 | // Honor explicit routing requests only if no client using default routing is active on this |
| 6578 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 6579 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6580 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6581 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6582 | if (device != nullptr) { |
| 6583 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 6584 | } |
| 6585 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 6586 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 6587 | // of setForceUse / Default Bus device here |
| 6588 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 6589 | if (device != nullptr) { |
| 6590 | return DeviceVector(device); |
| 6591 | } |
| 6592 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6593 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 6594 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
| 6595 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6596 | auto hasStreamActive = [&](auto stream) { |
| 6597 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 6598 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 6599 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6600 | auto doGetOutputDevicesForVoice = [&]() { |
| 6601 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 6602 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6603 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 6604 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 6605 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6606 | }; |
| 6607 | |
| 6608 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 6609 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 6610 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 6611 | // 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] | 6612 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 6613 | // stream is associated to the output descriptor. |
| 6614 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 6615 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 6616 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 6617 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6618 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 6619 | // check the route (would force modifying configuration file for this profile) |
| 6620 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 6621 | break; |
| 6622 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6623 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6624 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6625 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6626 | } |
| 6627 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6628 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 6629 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6630 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6631 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6632 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6633 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6634 | if (index >= 0) { |
| 6635 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6636 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6637 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 6638 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 6639 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6640 | } |
| 6641 | } |
| 6642 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6643 | // Honor explicit routing requests only if no client using default routing is active on this |
| 6644 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 6645 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6646 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 6647 | if (device != nullptr) { |
| 6648 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6649 | } |
| 6650 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 6651 | // 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] | 6652 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6653 | audio_attributes_t attributes; |
| 6654 | uid_t uid; |
| 6655 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 6656 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 6657 | attributes = topClient->attributes(); |
| 6658 | uid = topClient->uid(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6659 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 6660 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 6661 | uid = 0; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6662 | } |
| 6663 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6664 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 6665 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 6666 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6667 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6668 | device = mEngine->getInputDeviceForAttributes(attributes, uid); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 6669 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6670 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6671 | return device; |
| 6672 | } |
| 6673 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 6674 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 6675 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 6676 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 6677 | } |
| 6678 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6679 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6680 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6681 | if (devices == nullptr) { |
| 6682 | return BAD_VALUE; |
| 6683 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6684 | |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6685 | DeviceVector curDevices; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 6686 | if (status_t status = getDevicesForAttributes(attr, curDevices, forVolume); status != OK) { |
| 6687 | return status; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6688 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6689 | for (const auto& device : curDevices) { |
| 6690 | devices->push_back(device->getDeviceTypeAddr()); |
| 6691 | } |
| 6692 | return NO_ERROR; |
| 6693 | } |
| 6694 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6695 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6696 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6697 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6698 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6699 | updateDevicesAndOutputs(); |
| 6700 | break; |
| 6701 | default: |
| 6702 | break; |
| 6703 | } |
| 6704 | } |
| 6705 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6706 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 6707 | |
| 6708 | // skip beacon mute management if a dedicated TTS output is available |
| 6709 | if (mTtsOutputAvailable) { |
| 6710 | return 0; |
| 6711 | } |
| 6712 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6713 | switch(event) { |
| 6714 | case STARTING_OUTPUT: |
| 6715 | mBeaconMuteRefCount++; |
| 6716 | break; |
| 6717 | case STOPPING_OUTPUT: |
| 6718 | if (mBeaconMuteRefCount > 0) { |
| 6719 | mBeaconMuteRefCount--; |
| 6720 | } |
| 6721 | break; |
| 6722 | case STARTING_BEACON: |
| 6723 | mBeaconPlayingRefCount++; |
| 6724 | break; |
| 6725 | case STOPPING_BEACON: |
| 6726 | if (mBeaconPlayingRefCount > 0) { |
| 6727 | mBeaconPlayingRefCount--; |
| 6728 | } |
| 6729 | break; |
| 6730 | } |
| 6731 | |
| 6732 | if (mBeaconMuteRefCount > 0) { |
| 6733 | // any playback causes beacon to be muted |
| 6734 | return setBeaconMute(true); |
| 6735 | } else { |
| 6736 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 6737 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 6738 | } |
| 6739 | } |
| 6740 | |
| 6741 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 6742 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 6743 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 6744 | // keep track of muted state to avoid repeating mute/unmute operations |
| 6745 | if (mBeaconMuted != mute) { |
| 6746 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 6747 | ALOGV("\t muting %d", mute); |
| 6748 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 6749 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 6750 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 6751 | ALOGV("\t no tts volume source available"); |
| 6752 | return 0; |
| 6753 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6754 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6755 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6756 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6757 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 6758 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6759 | maxLatency = latency; |
| 6760 | } |
| 6761 | } |
| 6762 | mBeaconMuted = mute; |
| 6763 | return maxLatency; |
| 6764 | } |
| 6765 | return 0; |
| 6766 | } |
| 6767 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6768 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6769 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6770 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6771 | mPreviousOutputs = mOutputs; |
| 6772 | } |
| 6773 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 6774 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6775 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6776 | uint32_t delayMs) |
| 6777 | { |
| 6778 | // mute/unmute strategies using an incompatible device combination |
| 6779 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 6780 | // if unmuting, unmute only after the specified delay |
| 6781 | if (outputDesc->isDuplicated()) { |
| 6782 | return 0; |
| 6783 | } |
| 6784 | |
| 6785 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6786 | DeviceVector devices = outputDesc->devices(); |
| 6787 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6788 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6789 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 6790 | for (const auto &productStrategy : productStrategies) { |
| 6791 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 6792 | DeviceVector curDevices = |
| 6793 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 6794 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 6795 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6796 | bool doMute = false; |
| 6797 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6798 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6799 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6800 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 6801 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6802 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6803 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6804 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6805 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6806 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6807 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6808 | // 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] | 6809 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6810 | continue; |
| 6811 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6812 | ALOGVV("%s() %s (curDevice %s)", __func__, |
| 6813 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 6814 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 6815 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6816 | if (mute) { |
| 6817 | // FIXME: should not need to double latency if volume could be applied |
| 6818 | // immediately by the audioflinger mixer. We must account for the delay |
| 6819 | // between now and the next time the audioflinger thread for this output |
| 6820 | // will process a buffer (which corresponds to one buffer size, |
| 6821 | // usually 1/2 or 1/4 of the latency). |
| 6822 | if (muteWaitMs < desc->latency() * 2) { |
| 6823 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6824 | } |
| 6825 | } |
| 6826 | } |
| 6827 | } |
| 6828 | } |
| 6829 | } |
| 6830 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6831 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 6832 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6833 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 6834 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 6835 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 6836 | if (muteWaitMs < tempMuteWaitMs) { |
| 6837 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6838 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 6839 | |
| 6840 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 6841 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 6842 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 6843 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 6844 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 6845 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 6846 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6847 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 6848 | // make sure that we do not start the temporary mute period too early in case of |
| 6849 | // delayed device change |
| 6850 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 6851 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6852 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6853 | } |
| 6854 | } |
| 6855 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6856 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 6857 | if (muteWaitMs > delayMs) { |
| 6858 | muteWaitMs -= delayMs; |
| 6859 | usleep(muteWaitMs * 1000); |
| 6860 | return muteWaitMs; |
| 6861 | } |
| 6862 | return 0; |
| 6863 | } |
| 6864 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6865 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 6866 | const DeviceVector &devices, |
| 6867 | bool force, |
| 6868 | int delayMs, |
| 6869 | audio_patch_handle_t *patchHandle, |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6870 | bool requiresMuteCheck, bool requiresVolumeCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6871 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6872 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6873 | uint32_t muteWaitMs; |
| 6874 | |
| 6875 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6876 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 6877 | nullptr /* patchHandle */, requiresMuteCheck); |
| 6878 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 6879 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6880 | return muteWaitMs; |
| 6881 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6882 | |
| 6883 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6884 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 6885 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6886 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6887 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6888 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 6889 | |
| 6890 | if (!filteredDevices.isEmpty()) { |
| 6891 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6892 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6893 | |
| 6894 | // if the outputs are not materially active, there is no need to mute. |
| 6895 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6896 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6897 | } else { |
| 6898 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 6899 | muteWaitMs = 0; |
| 6900 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6901 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6902 | bool outputRouted = outputDesc->isRouted(); |
| 6903 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 6904 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 6905 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 6906 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6907 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 6908 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 6909 | // restore previous device after evaluating strategy mute state |
| 6910 | outputDesc->setDevices(prevDevices); |
| 6911 | return muteWaitMs; |
| 6912 | } |
| 6913 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6914 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 6915 | // the requested device is AUDIO_DEVICE_NONE |
| 6916 | // OR the requested device is the same as current device |
| 6917 | // AND force is not specified |
| 6918 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6919 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6920 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6921 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 6922 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6923 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
| 6924 | ALOGV("%s setting same device on routed output, force apply volumes", __func__); |
| 6925 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 6926 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6927 | return muteWaitMs; |
| 6928 | } |
| 6929 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6930 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6931 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6932 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6933 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6934 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6935 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6936 | PatchBuilder patchBuilder; |
| 6937 | patchBuilder.addSource(outputDesc); |
| 6938 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 6939 | for (const auto &filteredDevice : filteredDevices) { |
| 6940 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 6941 | } |
| 6942 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 6943 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 6944 | // to avoid disordered sequence of muting volume and changing devices. |
| 6945 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), |
| 6946 | muteWaitMs == 0 ? (delayMs + (outputDesc->latency() / 2)) : delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6947 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6948 | |
| 6949 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6950 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6951 | |
| 6952 | return muteWaitMs; |
| 6953 | } |
| 6954 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6955 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6956 | int delayMs, |
| 6957 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6958 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6959 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6960 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 6961 | return INVALID_OPERATION; |
| 6962 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6963 | if (patchHandle) { |
| 6964 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 6965 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 6966 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6967 | } |
| 6968 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6969 | return INVALID_OPERATION; |
| 6970 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6971 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6972 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6973 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 6974 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6975 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6976 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 6977 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6978 | return status; |
| 6979 | } |
| 6980 | |
| 6981 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6982 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6983 | bool force, |
| 6984 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6985 | { |
| 6986 | status_t status = NO_ERROR; |
| 6987 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6988 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6989 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 6990 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6991 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6992 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6993 | PatchBuilder patchBuilder; |
| 6994 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 6995 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 6996 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6997 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 6998 | auto result = usecase; |
| 6999 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 7000 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 7001 | } |
| 7002 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7003 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7004 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7005 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7006 | } |
| 7007 | } |
| 7008 | return status; |
| 7009 | } |
| 7010 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7011 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 7012 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7013 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 7014 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7015 | ssize_t index; |
| 7016 | if (patchHandle) { |
| 7017 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 7018 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 7019 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7020 | } |
| 7021 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7022 | return INVALID_OPERATION; |
| 7023 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7024 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7025 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7026 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 7027 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7028 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 7029 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 7030 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7031 | return status; |
| 7032 | } |
| 7033 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7034 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7035 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7036 | audio_format_t& format, |
| 7037 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7038 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7039 | { |
| 7040 | // Choose an input profile based on the requested capture parameters: select the first available |
| 7041 | // profile supporting all requested parameters. |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7042 | // 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] | 7043 | // |
| 7044 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 7045 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7046 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7047 | const audio_input_flags_t mustMatchFlag = AUDIO_INPUT_FLAG_MMAP_NOIRQ; |
| 7048 | const audio_input_flags_t oriFlags = flags; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7049 | |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7050 | for (;;) { |
| 7051 | sp<IOProfile> firstInexact = nullptr; |
| 7052 | uint32_t updatedSamplingRate = 0; |
| 7053 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 7054 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
| 7055 | for (const auto& hwModule : mHwModules) { |
| 7056 | for (const auto& profile : hwModule->getInputProfiles()) { |
| 7057 | // profile->log(); |
| 7058 | //updatedFormat = format; |
| 7059 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
| 7060 | &samplingRate /*updatedSamplingRate*/, |
| 7061 | format, |
| 7062 | &format, /*updatedFormat*/ |
| 7063 | channelMask, |
| 7064 | &channelMask /*updatedChannelMask*/, |
| 7065 | // FIXME ugly cast |
| 7066 | (audio_output_flags_t) flags, |
| 7067 | true /*exactMatchRequiredForInputFlags*/)) { |
| 7068 | return profile; |
| 7069 | } |
| 7070 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
| 7071 | samplingRate, |
| 7072 | &updatedSamplingRate, |
| 7073 | format, |
| 7074 | &updatedFormat, |
| 7075 | channelMask, |
| 7076 | &updatedChannelMask, |
| 7077 | // FIXME ugly cast |
| 7078 | (audio_output_flags_t) flags, |
| 7079 | false /*exactMatchRequiredForInputFlags*/)) { |
| 7080 | firstInexact = profile; |
| 7081 | } |
| 7082 | } |
| 7083 | } |
| 7084 | |
| 7085 | if (firstInexact != nullptr) { |
| 7086 | samplingRate = updatedSamplingRate; |
| 7087 | format = updatedFormat; |
| 7088 | channelMask = updatedChannelMask; |
| 7089 | return firstInexact; |
| 7090 | } else if (flags & AUDIO_INPUT_FLAG_RAW) { |
| 7091 | flags = (audio_input_flags_t) (flags & ~AUDIO_INPUT_FLAG_RAW); // retry |
| 7092 | } else if ((flags & mustMatchFlag) == AUDIO_INPUT_FLAG_NONE && |
| 7093 | flags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(format)) { |
| 7094 | flags = AUDIO_INPUT_FLAG_NONE; |
| 7095 | } else { // fail |
| 7096 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 7097 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 7098 | samplingRate, format, channelMask, oriFlags); |
| 7099 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7100 | } |
| 7101 | } |
jiabin | 2fd710d | 2022-05-02 23:20:22 +0000 | [diff] [blame] | 7102 | |
| 7103 | return nullptr; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7104 | } |
| 7105 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7106 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 7107 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 7108 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7109 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7110 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7111 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7112 | |
| 7113 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 7114 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 7115 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 7116 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7117 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7118 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 7119 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 7120 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 7121 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7122 | |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7123 | if (volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7124 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 7125 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 7126 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7127 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7128 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7129 | } |
| 7130 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 7131 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7132 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 7133 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7134 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7135 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 7136 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7137 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 7138 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 7139 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7140 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7141 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7142 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7143 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7144 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 7145 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7146 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 7147 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 7148 | // programmatically muted. |
| 7149 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 7150 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 7151 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7152 | bool exemptFromCapping = |
| 7153 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 7154 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7155 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 7156 | volumeSource, volumeDb); |
| 7157 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7158 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 7159 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 7160 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 7161 | } |
| 7162 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7163 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 7164 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 7165 | // - always attenuate notifications volume by 6dB |
| 7166 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 7167 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7168 | // - if music is playing, always limit the volume to current music volume, |
| 7169 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7170 | if (!Intersection(deviceTypes, |
| 7171 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 7172 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 7173 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 7174 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7175 | ((volumeSource == alarmVolumeSrc || |
| 7176 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7177 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 7178 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 7179 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7180 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 7181 | curves.canBeMuted()) { |
| 7182 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7183 | // when the phone is ringing we must consider that music could have been paused just before |
| 7184 | // by the music application and behave as if music was active if the last music track was |
| 7185 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7186 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7187 | mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7188 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7189 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7190 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 7191 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7192 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7193 | float musicVolDb = computeVolume(musicCurves, |
| 7194 | musicVolumeSrc, |
| 7195 | musicCurves.getVolumeIndex(musicDevice), |
| 7196 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7197 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 7198 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 7199 | if (volumeDb > minVolDb) { |
| 7200 | volumeDb = minVolDb; |
| 7201 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7202 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 7203 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 7204 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 7205 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7206 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 7207 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7208 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7209 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7210 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 7211 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7212 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 7213 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7214 | } |
| 7215 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7216 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7217 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7218 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7219 | } |
| 7220 | } |
| 7221 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7222 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7223 | } |
| 7224 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7225 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7226 | VolumeSource fromVolumeSource, |
| 7227 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7228 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7229 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7230 | return srcIndex; |
| 7231 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7232 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 7233 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7234 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 7235 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 7236 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 7237 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7238 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 7239 | // preserve mute request or correct range |
| 7240 | if (srcIndex < minSrc) { |
| 7241 | if (srcIndex == 0) { |
| 7242 | return 0; |
| 7243 | } |
| 7244 | srcIndex = minSrc; |
| 7245 | } else if (srcIndex > maxSrc) { |
| 7246 | srcIndex = maxSrc; |
| 7247 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7248 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 7249 | } |
| 7250 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7251 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 7252 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7253 | int index, |
| 7254 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7255 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7256 | int delayMs, |
| 7257 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7258 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7259 | // do not change actual attributes volume if the attributes is muted |
| 7260 | if (outputDesc->isMuted(volumeSource)) { |
| 7261 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 7262 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7263 | return NO_ERROR; |
| 7264 | } |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7265 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7266 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
| 7267 | bool isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 7268 | bool isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7269 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7270 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7271 | bool isHAUsed = isHearingAidUsedForComm(); |
| 7272 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7273 | // 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] | 7274 | // if sco and call follow same curves, bypass forceUseForComm |
| 7275 | if ((callVolSrc != btScoVolSrc) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7276 | ((isVoiceVolSrc && isScoRequested) || |
Eric Laurent | 1a8b45f | 2022-04-13 16:01:47 +0200 | [diff] [blame] | 7277 | (isBtScoVolSrc && !(isScoRequested || isHAUsed)))) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7278 | 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] | 7279 | volumeSource, isScoRequested ? " " : " not "); |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7280 | // Do not return an error here as AudioService will always set both voice call |
| 7281 | // and bluetooth SCO volumes due to stream aliasing. |
| 7282 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7283 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7284 | if (deviceTypes.empty()) { |
| 7285 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7286 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7287 | |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 7288 | if (curves.getVolumeIndexMin() < 0 || curves.getVolumeIndexMax() < 0) { |
| 7289 | ALOGE("invalid volume index range"); |
| 7290 | return BAD_VALUE; |
| 7291 | } |
| 7292 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7293 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 7294 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 7295 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 7296 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7297 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 7298 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7299 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7300 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7301 | outputDesc->setVolume( |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7302 | volumeDb, muted, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7303 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 7304 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7305 | float voiceVolume; |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7306 | // 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] | 7307 | if (isVoiceVolSrc) { |
| 7308 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7309 | } else { |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7310 | voiceVolume = index == 0 ? 0.0 : 1.0; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7311 | } |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 7312 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7313 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 7314 | mLastVoiceVolume = voiceVolume; |
| 7315 | } |
| 7316 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7317 | return NO_ERROR; |
| 7318 | } |
| 7319 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7320 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7321 | const DeviceTypeSet& deviceTypes, |
| 7322 | int delayMs, |
| 7323 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7324 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 7325 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7326 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 7327 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 7328 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7329 | curves.getVolumeIndex(deviceTypes), |
| 7330 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7331 | } |
| 7332 | } |
| 7333 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7334 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 7335 | bool on, |
| 7336 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7337 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7338 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7339 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7340 | std::vector<VolumeSource> sourcesToMute; |
| 7341 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 7342 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 7343 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7344 | VolumeSource source = toVolumeSource(attributes, false); |
| 7345 | if ((source != VOLUME_SOURCE_NONE) && |
| 7346 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 7347 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7348 | sourcesToMute.push_back(source); |
| 7349 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7350 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7351 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7352 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7353 | } |
| 7354 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7355 | } |
| 7356 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7357 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 7358 | bool on, |
| 7359 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7360 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7361 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7362 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7363 | if (deviceTypes.empty()) { |
| 7364 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7365 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7366 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7367 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7368 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7369 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7370 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7371 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 7372 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7373 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7374 | } |
| 7375 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7376 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 7377 | // ignored |
| 7378 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7379 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7380 | if (!outputDesc->isMuted(volumeSource)) { |
| 7381 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7382 | return; |
| 7383 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7384 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 7385 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7386 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7387 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7388 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7389 | delayMs); |
| 7390 | } |
| 7391 | } |
| 7392 | } |
| 7393 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7394 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 7395 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7396 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7397 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 7398 | return true; |
| 7399 | } |
| 7400 | |
| 7401 | // has known usage? |
| 7402 | switch (paa->usage) { |
| 7403 | case AUDIO_USAGE_UNKNOWN: |
| 7404 | case AUDIO_USAGE_MEDIA: |
| 7405 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7406 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7407 | case AUDIO_USAGE_ALARM: |
| 7408 | case AUDIO_USAGE_NOTIFICATION: |
| 7409 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7410 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7411 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7412 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7413 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7414 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7415 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7416 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7417 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7418 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 7419 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 7420 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 7421 | case AUDIO_USAGE_EMERGENCY: |
| 7422 | case AUDIO_USAGE_SAFETY: |
| 7423 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 7424 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7425 | break; |
| 7426 | default: |
| 7427 | return false; |
| 7428 | } |
| 7429 | return true; |
| 7430 | } |
| 7431 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7432 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 7433 | { |
| 7434 | return mEngine->getForceUse(usage); |
| 7435 | } |
| 7436 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7437 | bool AudioPolicyManager::isInCall() const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7438 | return isStateInCall(mEngine->getPhoneState()); |
| 7439 | } |
| 7440 | |
Eric Laurent | 96d1dda | 2022-03-14 17:14:19 +0100 | [diff] [blame] | 7441 | bool AudioPolicyManager::isStateInCall(int state) const { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7442 | return is_state_in_call(state); |
| 7443 | } |
| 7444 | |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7445 | bool AudioPolicyManager::isCallAudioAccessible() |
| 7446 | { |
| 7447 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7448 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7449 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 7450 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7451 | } |
| 7452 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7453 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 7454 | { |
| 7455 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 7456 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7457 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 7458 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 7459 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7460 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7461 | } |
| 7462 | } |
| 7463 | |
| 7464 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 7465 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 7466 | bool release = false; |
| 7467 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 7468 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 7469 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 7470 | source->ext.device.type == deviceDesc->type()) { |
| 7471 | release = true; |
| 7472 | } |
| 7473 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7474 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7475 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 7476 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 7477 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7478 | sink->ext.device.type == deviceDesc->type() && |
| 7479 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 7480 | || strncmp(sink->ext.device.address, address, |
| 7481 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7482 | release = true; |
| 7483 | } |
| 7484 | } |
| 7485 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7486 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 7487 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7488 | } |
| 7489 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7490 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7491 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 7492 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7493 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7494 | } |
| 7495 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7496 | void AudioPolicyManager::modifySurroundFormats( |
| 7497 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7498 | std::unordered_set<audio_format_t> enforcedSurround( |
| 7499 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7500 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 7501 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 7502 | allSurround.insert(pair.first); |
| 7503 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 7504 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7505 | |
| 7506 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7507 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7508 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7509 | // This is the resulting set of formats depending on the surround mode: |
| 7510 | // 'all surround' = allSurround |
| 7511 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 7512 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 7513 | // 'manual surround' = mManualSurroundFormats |
| 7514 | // AUTO: formats v 'enforced surround' |
| 7515 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 7516 | // NEVER: formats ^ 'non-surround' |
| 7517 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7518 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7519 | std::unordered_set<audio_format_t> formatSet; |
| 7520 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 7521 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7522 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7523 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7524 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7525 | formatSet.insert(*formatIter); |
| 7526 | } |
| 7527 | } |
| 7528 | } else { |
| 7529 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 7530 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7531 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7532 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7533 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7534 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7535 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 7536 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 7537 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7538 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7539 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 7540 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 7541 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 7542 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7543 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7544 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7545 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7546 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7547 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7548 | } |
| 7549 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7550 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 7551 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7552 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7553 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 7554 | |
| 7555 | // If NEVER, then remove support for channelMasks > stereo. |
| 7556 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7557 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 7558 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7559 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 7560 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7561 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7562 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7563 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7564 | } |
| 7565 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7566 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 7567 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 7568 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7569 | bool supports5dot1 = false; |
| 7570 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 7571 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7572 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 7573 | supports5dot1 = true; |
| 7574 | break; |
| 7575 | } |
| 7576 | } |
| 7577 | // If not then add 5.1 support. |
| 7578 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7579 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 7580 | 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] | 7581 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7582 | } |
| 7583 | } |
| 7584 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7585 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 7586 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7587 | AudioProfileVector &profiles) |
| 7588 | { |
| 7589 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7590 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7591 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7592 | // Format MUST be checked first to update the list of AudioProfile |
| 7593 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7594 | reply = mpClientInterface->getParameters( |
| 7595 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7596 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7597 | AudioParameter repliedParameters(reply); |
| 7598 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7599 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7600 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 7601 | return; |
| 7602 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7603 | FormatVector formats = formatsFromString(reply.string()); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 7604 | mReportedFormatsMap[devDesc] = formats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7605 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 7606 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7607 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 7608 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 7609 | addProfilesForFormats(profiles, formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7610 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7611 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 7612 | for (audio_format_t format : profiles.getSupportedFormats()) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7613 | ChannelMaskSet channelMasks; |
| 7614 | SampleRateSet samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7615 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7616 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7617 | |
| 7618 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7619 | reply = mpClientInterface->getParameters( |
| 7620 | ioHandle, |
| 7621 | requestedParameters.toString() + ";" + |
| 7622 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7623 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7624 | AudioParameter repliedParameters(reply); |
| 7625 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7626 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7627 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7628 | } |
| 7629 | } |
| 7630 | if (profiles.hasDynamicChannelsFor(format)) { |
| 7631 | reply = mpClientInterface->getParameters(ioHandle, |
| 7632 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7633 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7634 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7635 | AudioParameter repliedParameters(reply); |
| 7636 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7637 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7638 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7639 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 7640 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7641 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7642 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7643 | } |
| 7644 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 7645 | addDynamicAudioProfileAndSort( |
| 7646 | profiles, new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7647 | } |
| 7648 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7649 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7650 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 7651 | audio_patch_handle_t *patchHandle, |
| 7652 | AudioIODescriptorInterface *ioDescriptor, |
| 7653 | const struct audio_patch *patch, |
| 7654 | int delayMs) |
| 7655 | { |
| 7656 | ssize_t index = mAudioPatches.indexOfKey( |
| 7657 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 7658 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 7659 | sp<AudioPatch> patchDesc; |
| 7660 | status_t status = installPatch( |
| 7661 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 7662 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7663 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7664 | } |
| 7665 | return status; |
| 7666 | } |
| 7667 | |
| 7668 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 7669 | ssize_t index, |
| 7670 | audio_patch_handle_t *patchHandle, |
| 7671 | const struct audio_patch *patch, |
| 7672 | int delayMs, |
| 7673 | uid_t uid, |
| 7674 | sp<AudioPatch> *patchDescPtr) |
| 7675 | { |
| 7676 | sp<AudioPatch> patchDesc; |
| 7677 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 7678 | if (index >= 0) { |
| 7679 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7680 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7681 | } |
| 7682 | |
| 7683 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 7684 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 7685 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 7686 | if (status == NO_ERROR) { |
| 7687 | if (index < 0) { |
| 7688 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7689 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7690 | } else { |
| 7691 | patchDesc->mPatch = *patch; |
| 7692 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7693 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7694 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7695 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7696 | } |
| 7697 | nextAudioPortGeneration(); |
| 7698 | mpClientInterface->onAudioPatchListUpdate(); |
| 7699 | } |
| 7700 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 7701 | return status; |
| 7702 | } |
| 7703 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7704 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 7705 | { |
| 7706 | const TrackClientVector activeClients = output->getActiveClients(); |
| 7707 | if (activeClients.empty()) { |
| 7708 | return true; |
| 7709 | } |
| 7710 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 7711 | if (index < 0) { |
| 7712 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 7713 | __func__, output->getId()); |
| 7714 | return false; |
| 7715 | } |
| 7716 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 7717 | DeviceVector routedDevices; |
| 7718 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 7719 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 7720 | patchDesc->mPatch.sinks[i].id); |
| 7721 | if (device == nullptr) { |
| 7722 | ALOGE("%s, no audio device found with id(%d)", |
| 7723 | __func__, patchDesc->mPatch.sinks[i].id); |
| 7724 | return false; |
| 7725 | } |
| 7726 | routedDevices.add(device); |
| 7727 | } |
| 7728 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 7729 | if (client->isInvalid()) { |
| 7730 | // No need to take care about invalidated clients. |
| 7731 | continue; |
| 7732 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7733 | sp<DeviceDescriptor> preferredDevice = |
| 7734 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 7735 | if (mEngine->getOutputDevicesForAttributes( |
| 7736 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 7737 | return false; |
| 7738 | } |
| 7739 | } |
| 7740 | return true; |
| 7741 | } |
| 7742 | |
| 7743 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7744 | const sp<IOProfile>& profile, const DeviceVector& devices, |
| 7745 | const audio_config_base_t *mixerConfig) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7746 | { |
| 7747 | for (const auto& device : devices) { |
| 7748 | // TODO: This should be checking if the profile supports the device combo. |
| 7749 | if (!profile->supportsDevice(device)) { |
| 7750 | return nullptr; |
| 7751 | } |
| 7752 | } |
| 7753 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 7754 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7755 | status_t status = desc->open(nullptr /* halConfig */, mixerConfig, devices, |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7756 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
| 7757 | if (status != NO_ERROR) { |
| 7758 | return nullptr; |
| 7759 | } |
| 7760 | |
| 7761 | // Here is where the out_set_parameters() for card & device gets called |
| 7762 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 7763 | const audio_devices_t deviceType = device->type(); |
| 7764 | const String8 &address = String8(device->address().c_str()); |
| 7765 | if (!address.isEmpty()) { |
| 7766 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 7767 | mpClientInterface->setParameters(output, String8(param)); |
| 7768 | free(param); |
| 7769 | } |
| 7770 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
| 7771 | if (!profile->hasValidAudioProfile()) { |
| 7772 | ALOGW("%s() missing param", __func__); |
| 7773 | desc->close(); |
| 7774 | return nullptr; |
| 7775 | } else if (profile->hasDynamicAudioProfile()) { |
| 7776 | desc->close(); |
| 7777 | output = AUDIO_IO_HANDLE_NONE; |
| 7778 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 7779 | profile->pickAudioProfile( |
| 7780 | config.sample_rate, config.channel_mask, config.format); |
| 7781 | config.offload_info.sample_rate = config.sample_rate; |
| 7782 | config.offload_info.channel_mask = config.channel_mask; |
| 7783 | config.offload_info.format = config.format; |
| 7784 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7785 | status = desc->open(&config, mixerConfig, devices, |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7786 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
| 7787 | if (status != NO_ERROR) { |
| 7788 | return nullptr; |
| 7789 | } |
| 7790 | } |
| 7791 | |
| 7792 | addOutput(output, desc); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7793 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 7794 | sp<DeviceDescriptor> speaker = mAvailableOutputDevices.getDevice( |
| 7795 | AUDIO_DEVICE_OUT_SPEAKER, String8(""), AUDIO_FORMAT_DEFAULT); |
| 7796 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7797 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 7798 | sp<AudioPolicyMix> policyMix; |
| 7799 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 7800 | policyMix->setOutput(desc); |
| 7801 | desc->mPolicyMix = policyMix; |
| 7802 | } else { |
| 7803 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
| 7804 | address.string()); |
| 7805 | } |
| 7806 | |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 7807 | } else if (hasPrimaryOutput() && speaker != nullptr |
| 7808 | && mPrimaryOutput->supportsDevice(speaker) && !desc->supportsDevice(speaker) |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7809 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 7810 | // no duplicated output for: |
| 7811 | // - direct outputs |
| 7812 | // - outputs used by dynamic policy mixes |
baek.kim - | 61c2012 | 2022-07-27 10:05:32 +0000 | [diff] [blame] | 7813 | // - outputs that supports SPEAKER while the primary output does not. |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7814 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 7815 | |
| 7816 | //TODO: configure audio effect output stage here |
| 7817 | |
| 7818 | // open a duplicating output thread for the new output and the primary output |
| 7819 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 7820 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 7821 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 7822 | if (status == NO_ERROR) { |
| 7823 | // add duplicated output descriptor |
| 7824 | addOutput(duplicatedOutput, dupOutputDesc); |
| 7825 | } else { |
| 7826 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 7827 | mPrimaryOutput->mIoHandle, output); |
| 7828 | desc->close(); |
| 7829 | removeOutput(output); |
| 7830 | nextAudioPortGeneration(); |
| 7831 | return nullptr; |
| 7832 | } |
| 7833 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 7834 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 7835 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 7836 | mPrimaryOutput = desc; |
| 7837 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7838 | return desc; |
| 7839 | } |
| 7840 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 7841 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 7842 | const audio_attributes_t &attr, DeviceVector &devices, bool forVolume) { |
| 7843 | // Devices are determined in the following precedence: |
| 7844 | // |
| 7845 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 7846 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 7847 | // |
| 7848 | // If no such dynamic policy then |
| 7849 | // 2) Devices containing an active client using setPreferredDevice |
| 7850 | // with same strategy as the attributes. |
| 7851 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 7852 | // |
| 7853 | // If no corresponding active client with setPreferredDevice then |
| 7854 | // 3) Devices associated with the strategy determined by the attributes |
| 7855 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 7856 | // |
| 7857 | // See related getOutputForAttrInt(). |
| 7858 | |
| 7859 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 7860 | // audio routing, only used for duplication for playback capture) |
| 7861 | sp<AudioPolicyMix> policyMix; |
| 7862 | status_t status = mPolicyMixes.getOutputForAttr(attr, AUDIO_CONFIG_BASE_INITIALIZER, |
| 7863 | 0 /*uid unknown here*/, AUDIO_OUTPUT_FLAG_NONE, policyMix, |
| 7864 | nullptr /* secondaryMixes */); |
| 7865 | if (status != OK) { |
| 7866 | return status; |
| 7867 | } |
| 7868 | |
| 7869 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 7870 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 7871 | // as they are unaffected by device/stream volume |
| 7872 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 7873 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 7874 | ) { |
| 7875 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 7876 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 7877 | devices.add(deviceDesc); |
| 7878 | } else { |
| 7879 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 7880 | // which selects setPreferredDevice if active. This means forVolume call |
| 7881 | // will take an active setPreferredDevice, if such exists. |
| 7882 | |
| 7883 | devices = mEngine->getOutputDevicesForAttributes( |
| 7884 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
| 7885 | } |
| 7886 | |
| 7887 | if (forVolume) { |
| 7888 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 7889 | // for single volume control in AudioService (such relationship should exist if |
| 7890 | // SPEAKER_SAFE is present). |
| 7891 | // |
| 7892 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 7893 | DeviceVector speakerSafeDevices = |
| 7894 | devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 7895 | if (!speakerSafeDevices.isEmpty()) { |
| 7896 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 7897 | devices.remove(speakerSafeDevices); |
| 7898 | } |
| 7899 | } |
| 7900 | |
| 7901 | return NO_ERROR; |
| 7902 | } |
| 7903 | |
| 7904 | status_t AudioPolicyManager::getProfilesForDevices(const DeviceVector& devices, |
| 7905 | AudioProfileVector& audioProfiles, |
| 7906 | uint32_t flags, |
| 7907 | bool isInput) { |
| 7908 | for (const auto& hwModule : mHwModules) { |
| 7909 | // the MSD module checks for different conditions |
| 7910 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 7911 | continue; |
| 7912 | } |
| 7913 | IOProfileCollection ioProfiles = isInput ? hwModule->getInputProfiles() |
| 7914 | : hwModule->getOutputProfiles(); |
| 7915 | for (const auto& profile : ioProfiles) { |
| 7916 | if (!profile->areAllDevicesSupported(devices) || |
| 7917 | !profile->isCompatibleProfileForFlags( |
| 7918 | flags, false /*exactMatchRequiredForInputFlags*/)) { |
| 7919 | continue; |
| 7920 | } |
| 7921 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 7922 | } |
| 7923 | } |
| 7924 | |
| 7925 | if (!isInput) { |
| 7926 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 7927 | const auto &msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 7928 | if (msdModule != nullptr) { |
| 7929 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 7930 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 7931 | for (const auto &profile: msdModule->getOutputProfiles()) { |
| 7932 | if (!profile->asAudioPort()->isDirectOutput()) { |
| 7933 | continue; |
| 7934 | } |
| 7935 | audioProfiles.addAllValidProfiles(profile->asAudioPort()->getAudioProfiles()); |
| 7936 | } |
| 7937 | } else { |
| 7938 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 7939 | } |
| 7940 | } |
| 7941 | } |
| 7942 | |
| 7943 | return NO_ERROR; |
| 7944 | } |
| 7945 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 7946 | } // namespace android |