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) { |
| 143 | // TODO: b/211601178 Forward 'port' to Audio HAL via mHwModules. For now, only device_type, |
| 144 | // device_address and device_name are forwarded. |
| 145 | if (port.ext.getTag() != AudioPortExt::device) { |
| 146 | return BAD_VALUE; |
| 147 | } |
| 148 | audio_devices_t device_type; |
| 149 | std::string device_address; |
| 150 | if (status_t status = aidl2legacy_AudioDevice_audio_device( |
| 151 | port.ext.get<AudioPortExt::device>().device, &device_type, &device_address); |
| 152 | status != OK) { |
| 153 | return status; |
| 154 | }; |
| 155 | const char* device_name = port.name.c_str(); |
| 156 | // connect/disconnect only 1 device at a time |
| 157 | if (!audio_is_output_device(device_type) && !audio_is_input_device(device_type)) |
| 158 | return BAD_VALUE; |
| 159 | |
| 160 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 161 | device_type, device_address.c_str(), device_name, encodedFormat, |
| 162 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 163 | return device ? setDeviceConnectionStateInt(device, state) : INVALID_OPERATION; |
| 164 | } |
| 165 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 166 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 167 | audio_policy_dev_state_t state, |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 168 | const char* device_address, |
| 169 | const char* device_name, |
| 170 | audio_format_t encodedFormat) { |
| 171 | media::AudioPort aidlPort; |
| 172 | if (status_t status = deviceToAudioPort(deviceType, device_address, device_name, &aidlPort); |
| 173 | status == OK) { |
| 174 | return setDeviceConnectionStateInt(state, aidlPort.hal, encodedFormat); |
| 175 | } else { |
| 176 | ALOGE("Failed to convert to AudioPort Parcelable: %s", statusToString(status).c_str()); |
| 177 | return status; |
| 178 | } |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 179 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 180 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 181 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 182 | audio_policy_dev_state_t state) |
| 183 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 185 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 186 | SortedVector <audio_io_handle_t> outputs; |
| 187 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 188 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 189 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 190 | // save a copy of the opened output descriptors before any output is opened or closed |
| 191 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 192 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 193 | switch (state) |
| 194 | { |
| 195 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 196 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 197 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 198 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 199 | return INVALID_OPERATION; |
| 200 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 201 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 202 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 203 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 204 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 205 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 206 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 207 | } |
| 208 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 209 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 210 | // parameters on newly connected devices (instead of opening the outputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 211 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 212 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 213 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 214 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 215 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 216 | mHwModules.cleanUpForDevice(device); |
| 217 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 218 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 219 | return INVALID_OPERATION; |
| 220 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 221 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 222 | // Populate encapsulation information when a output device is connected. |
| 223 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 224 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 225 | // outputs should never be empty here |
| 226 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 227 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 228 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 229 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 230 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 231 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 232 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 233 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 234 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 235 | return INVALID_OPERATION; |
| 236 | } |
| 237 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 238 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 239 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 240 | // Send Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 241 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 242 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 243 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 244 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 245 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 246 | mOutputs.clearSessionRoutesForDevice(device); |
| 247 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 248 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 249 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 250 | // Reset active device codec |
| 251 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 252 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 253 | // remove device from mReportedFormatsMap cache |
| 254 | mReportedFormatsMap.erase(device); |
| 255 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 256 | } break; |
| 257 | |
| 258 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 259 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 260 | return BAD_VALUE; |
| 261 | } |
| 262 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 263 | // Propagate device availability to Engine |
| 264 | setEngineDeviceConnectionState(device, state); |
| 265 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 266 | // No need to evaluate playback routing when connecting a remote submix |
| 267 | // output device used by a dynamic policy of type recorder as no |
| 268 | // playback use case is affected. |
| 269 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 270 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 271 | for (audio_io_handle_t output : outputs) { |
| 272 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 273 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 274 | if (policyMix != nullptr |
| 275 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 276 | && device->address() == policyMix->mDeviceAddress.string()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 277 | doCheckForDeviceAndOutputChanges = false; |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 284 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 285 | if (!outputs.isEmpty()) { |
| 286 | for (audio_io_handle_t output : outputs) { |
| 287 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 288 | // close unused outputs after device disconnection or direct outputs that have |
| 289 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 290 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) |
| 291 | || (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 292 | (desc->mDirectOpenCount == 0))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 293 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 294 | closeOutput(output); |
| 295 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 296 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 297 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 298 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 299 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 300 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | if (doCheckForDeviceAndOutputChanges) { |
| 304 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 305 | } else { |
| 306 | checkCloseOutputs(); |
| 307 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 308 | (void)updateCallRouting(false /*fromCache*/); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 309 | std::vector<audio_io_handle_t> outputsToReopen; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 310 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 311 | const DeviceVector activeMediaDevices = |
| 312 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 313 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 314 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 315 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 316 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 317 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 318 | // do not force device change on duplicated output because if device is 0, it will |
| 319 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 320 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 321 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 322 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 323 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 324 | // always force when disconnecting (a non-duplicated device) |
| 325 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 326 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 327 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 328 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 329 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 330 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 331 | // Reopen the output to query the dynamic profiles when there is not active |
| 332 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 333 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 334 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 335 | if (areAllActiveTracksRerouted(desc)) { |
| 336 | outputsToReopen.push_back(mOutputs.keyAt(i)); |
| 337 | } else { |
| 338 | desc->mPendingReopenToQueryProfiles = true; |
| 339 | } |
| 340 | } |
| 341 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 342 | // Clear the flag that previously set for re-querying profiles. |
| 343 | desc->mPendingReopenToQueryProfiles = false; |
| 344 | } |
| 345 | } |
| 346 | for (const auto& output : outputsToReopen) { |
| 347 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 348 | closeOutput(output); |
| 349 | openOutputWithProfileAndDevice(desc->mProfile, activeMediaDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 352 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 353 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 356 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 357 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 358 | } // end if is output device |
| 359 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 361 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 362 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 363 | switch (state) |
| 364 | { |
| 365 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 366 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 367 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 368 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 369 | return INVALID_OPERATION; |
| 370 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 371 | |
| 372 | if (mAvailableInputDevices.add(device) < 0) { |
| 373 | return NO_MEMORY; |
| 374 | } |
| 375 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 376 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 377 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 378 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 379 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 380 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 381 | mAvailableInputDevices.remove(device); |
| 382 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 383 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 384 | |
| 385 | mHwModules.cleanUpForDevice(device); |
| 386 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 387 | return INVALID_OPERATION; |
| 388 | } |
| 389 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 390 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 391 | |
| 392 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 393 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 394 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 395 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 396 | return INVALID_OPERATION; |
| 397 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 398 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 399 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 400 | |
| 401 | // Set Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 402 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 403 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 404 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 405 | |
| 406 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 407 | |
| 408 | // remove device from mReportedFormatsMap cache |
| 409 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 410 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 411 | |
| 412 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 413 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 414 | return BAD_VALUE; |
| 415 | } |
| 416 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 417 | // Propagate device availability to Engine |
| 418 | setEngineDeviceConnectionState(device, state); |
| 419 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 420 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 421 | // As the input device list can impact the output device selection, update |
| 422 | // getDeviceForStrategy() cache |
| 423 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 424 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 425 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 426 | // Reconnect Audio Source |
| 427 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 428 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 429 | checkAudioSourceForAttributes(attributes); |
| 430 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 431 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 432 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 435 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 436 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 437 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 438 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 439 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 440 | return BAD_VALUE; |
| 441 | } |
| 442 | |
Nathalie Le Clair | 88fa275 | 2021-11-23 13:03:41 +0100 | [diff] [blame] | 443 | status_t AudioPolicyManager::deviceToAudioPort(audio_devices_t device, const char* device_address, |
| 444 | const char* device_name, |
| 445 | media::AudioPort* aidlPort) { |
| 446 | DeviceDescriptorBase devDescr(device, device_address); |
| 447 | devDescr.setName(device_name); |
| 448 | return devDescr.writeToParcelable(aidlPort); |
| 449 | } |
| 450 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 451 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 452 | audio_policy_dev_state_t state) { |
| 453 | |
| 454 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 455 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 456 | return; |
| 457 | } |
| 458 | mEngine->setDeviceConnectionState(device, state); |
| 459 | } |
| 460 | |
| 461 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 462 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 463 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 464 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 465 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 466 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 467 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 468 | (strlen(device_address) != 0)/*matchAddress*/); |
| 469 | |
| 470 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 471 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 472 | device, device_address); |
| 473 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 474 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 475 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 476 | DeviceVector *deviceVector; |
| 477 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 478 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 479 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 480 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 481 | deviceVector = &mAvailableInputDevices; |
| 482 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 483 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 484 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 485 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 486 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 487 | return (deviceVector->getDevice( |
| 488 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 489 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 492 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 493 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 494 | const char *device_name, |
| 495 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 496 | { |
| 497 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 498 | String8 reply; |
| 499 | AudioParameter param; |
| 500 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 501 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 502 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 503 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 504 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 505 | // connect/disconnect only 1 device at a time |
| 506 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 507 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 508 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 509 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 510 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 511 | // Nothing to do: device is not connected |
| 512 | return NO_ERROR; |
| 513 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 514 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 515 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 516 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 517 | // configure codecs. |
| 518 | // Handle two specific cases by sending a set parameter to |
| 519 | // configure A2DP codecs. No need to toggle device state. |
| 520 | // Case 1: A2DP active device switches from primary to primary |
| 521 | // module |
| 522 | // Case 2: A2DP device config changes on primary module. |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 523 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 524 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 525 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 526 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 527 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 528 | reply = mpClientInterface->getParameters( |
| 529 | AUDIO_IO_HANDLE_NONE, |
| 530 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 531 | AudioParameter repliedParameters(reply); |
| 532 | repliedParameters.getInt( |
| 533 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 534 | if (isReconfigA2dpSupported) { |
| 535 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 536 | param.add(key, String8("true")); |
| 537 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 538 | devDesc->setEncodedFormat(encodedFormat); |
| 539 | return NO_ERROR; |
| 540 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 543 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 544 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 545 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 546 | // mute media strategies and delay device switch by the largest |
| 547 | // This avoid sending the music tail into the earpiece or headset. |
| 548 | setStrategyMute(musicStrategy, true, desc); |
| 549 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 550 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 551 | nullptr, true /*fromCache*/).types()); |
| 552 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 553 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 554 | // This will force reading again the device configuration |
| 555 | status = setDeviceConnectionState(device, |
| 556 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 557 | device_address, device_name, |
| 558 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 559 | if (status != NO_ERROR) { |
| 560 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 561 | status); |
| 562 | return status; |
| 563 | } |
| 564 | |
| 565 | status = setDeviceConnectionState(device, |
| 566 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 567 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 568 | if (status != NO_ERROR) { |
| 569 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 570 | status); |
| 571 | return status; |
| 572 | } |
| 573 | |
| 574 | return NO_ERROR; |
| 575 | } |
| 576 | |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 577 | status_t AudioPolicyManager::getHwOffloadFormatsSupportedForBluetoothMedia( |
| 578 | audio_devices_t device, std::vector<audio_format_t> *formats) |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 579 | { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 580 | ALOGV("getHwOffloadFormatsSupportedForBluetoothMedia()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 581 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 582 | std::unordered_set<audio_format_t> formatSet; |
| 583 | sp<HwModule> primaryModule = |
| 584 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 585 | if (primaryModule == nullptr) { |
| 586 | ALOGE("%s() unable to get primary module", __func__); |
| 587 | return NO_INIT; |
| 588 | } |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 589 | |
| 590 | DeviceTypeSet audioDeviceSet; |
| 591 | |
| 592 | switch(device) { |
| 593 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 594 | audioDeviceSet = getAudioDeviceOutAllA2dpSet(); |
| 595 | break; |
| 596 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
| 597 | audioDeviceSet = getAudioDeviceOutAllBleSet(); |
| 598 | break; |
| 599 | default: |
| 600 | ALOGE("%s() device type 0x%08x not supported", __func__, device); |
| 601 | return BAD_VALUE; |
| 602 | } |
| 603 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 604 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 605 | audioDeviceSet); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 606 | for (const auto& device : declaredDevices) { |
| 607 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 608 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 609 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 610 | return status; |
| 611 | } |
| 612 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 613 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 614 | { |
| 615 | DeviceVector rxSinkdevices{}; |
| 616 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 617 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 618 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 619 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 620 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 621 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 622 | // retrieve Rx Source device descriptor |
| 623 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 624 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 625 | |
| 626 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 627 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 628 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 629 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 630 | return DeviceVector(rxSinkDevice); |
| 631 | } |
| 632 | } |
| 633 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 634 | // the device returned is not necessarily reachable via this output |
| 635 | // (filter later by setOutputDevices()) |
| 636 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 637 | } |
| 638 | |
| 639 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 640 | { |
| 641 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
| 642 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 643 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 644 | } |
| 645 | return INVALID_OPERATION; |
| 646 | } |
| 647 | |
| 648 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 649 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 650 | { |
| 651 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 652 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 653 | uint32_t muteWaitMs = 0; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 654 | if(!hasPrimaryOutput() || |
| 655 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 656 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 657 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 658 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 659 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 660 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 661 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 662 | ALOG_ASSERT(txSourceDevice != 0, "%s() input selected device not available", __func__); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 663 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 664 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 665 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 666 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 667 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 668 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 669 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 670 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 671 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 672 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 673 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 674 | // retrieve Rx Source and Tx Sink device descriptors |
| 675 | sp<DeviceDescriptor> rxSourceDevice = |
| 676 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 677 | String8(), |
| 678 | AUDIO_FORMAT_DEFAULT); |
| 679 | sp<DeviceDescriptor> txSinkDevice = |
| 680 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 681 | String8(), |
| 682 | AUDIO_FORMAT_DEFAULT); |
| 683 | |
| 684 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 685 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 686 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 687 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 688 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 689 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 690 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 691 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 692 | // createAudioPatchInternal now supports both HW / SW bridging |
| 693 | createRxPatch = true; |
| 694 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 695 | } else { |
| 696 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 697 | // voice calls via setOutputDevice() on primary output. |
| 698 | // Otherwise, create two audio patches for TX and RX path. |
| 699 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 700 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 701 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 702 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 703 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 704 | (txSinkDevice != 0); |
| 705 | } |
| 706 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 707 | // Otherwise, create two audio patches for TX and RX path. |
| 708 | if (!createRxPatch) { |
| 709 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 710 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 711 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 712 | // If the TX device is on the primary HW module but RX device is |
| 713 | // on other HW module, SinkMetaData of telephony input should handle it |
| 714 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 715 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 716 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 717 | // terminate active capture if on the same HW module as the call TX source device |
| 718 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 719 | // call TX device but this information is not in the audio patch and logic here must be |
| 720 | // symmetric to the one in startInput() |
| 721 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 722 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 723 | closeActiveClients(activeDesc); |
| 724 | } |
| 725 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 726 | connectTelephonyTxAudioSource(txSourceDevice, txSinkDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 727 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 728 | if (waitMs != nullptr) { |
| 729 | *waitMs = muteWaitMs; |
| 730 | } |
| 731 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 732 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 733 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 734 | bool AudioPolicyManager::isDeviceOfModule( |
| 735 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 736 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 737 | if (module != 0) { |
| 738 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 739 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 740 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 741 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 746 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 747 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 748 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 749 | const struct audio_port_config source = { |
| 750 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 751 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 752 | }; |
| 753 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 754 | mCallRxSourceClient = startAudioSourceInternal(&source, &aa, 0/*uid*/); |
| 755 | ALOGE_IF(mCallRxSourceClient == nullptr, |
| 756 | "%s failed to start Telephony Rx AudioSource", __func__); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 759 | void AudioPolicyManager::disconnectTelephonyAudioSource(sp<SourceClientDescriptor> &clientDesc) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 760 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 761 | if (clientDesc == nullptr) { |
| 762 | return; |
| 763 | } |
| 764 | ALOGW_IF(stopAudioSource(clientDesc->portId()) != NO_ERROR, |
| 765 | "%s error stopping audio source", __func__); |
| 766 | clientDesc.clear(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | void AudioPolicyManager::connectTelephonyTxAudioSource( |
| 770 | const sp<DeviceDescriptor> &srcDevice, const sp<DeviceDescriptor> &sinkDevice, |
| 771 | uint32_t delayMs) |
| 772 | { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 773 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 774 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 775 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 776 | return; |
| 777 | } |
| 778 | PatchBuilder patchBuilder; |
| 779 | patchBuilder.addSource(srcDevice).addSink(sinkDevice); |
| 780 | ALOGV("%s between source %s and sink %s", __func__, |
| 781 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 782 | auto callTxSourceClientPortId = PolicyAudioPort::getNextUniqueId(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 783 | const audio_attributes_t aa = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
| 784 | struct audio_port_config source = {}; |
| 785 | srcDevice->toAudioPortConfig(&source); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 786 | mCallTxSourceClient = new InternalSourceClientDescriptor( |
| 787 | callTxSourceClientPortId, mUidCached, aa, source, srcDevice, sinkDevice, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 788 | mCommunnicationStrategy, toVolumeSource(aa)); |
| 789 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 790 | status_t status = connectAudioSourceToSink( |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 791 | mCallTxSourceClient, sinkDevice, patchBuilder.patch(), patchHandle, mUidCached, |
| 792 | delayMs); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 793 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating TX audio patch", __func__, status); |
| 794 | if (status == NO_ERROR) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 795 | mAudioSources.add(callTxSourceClientPortId, mCallTxSourceClient); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 796 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 799 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 800 | { |
| 801 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 802 | // store previous phone state for management of sonification strategy below |
| 803 | int oldState = mEngine->getPhoneState(); |
| 804 | |
| 805 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 806 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 807 | return; |
| 808 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 809 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 810 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 811 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 812 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 813 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 814 | } |
| 815 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 816 | /** |
| 817 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 818 | * routing command. |
| 819 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 820 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 821 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 822 | |
| 823 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 824 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 825 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 826 | int delayMs = 0; |
| 827 | if (isStateInCall(state)) { |
| 828 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 829 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 830 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 831 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 832 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 833 | // mute media and sonification strategies and delay device switch by the largest |
| 834 | // latency of any output where either strategy is active. |
| 835 | // 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] | 836 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 837 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 838 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 839 | (delayMs < (int)desc->latency()*2)) { |
| 840 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 841 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 842 | setStrategyMute(musicStrategy, true, desc); |
| 843 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 844 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 845 | nullptr, true /*fromCache*/).types()); |
| 846 | setStrategyMute(sonificationStrategy, true, desc); |
| 847 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 848 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 849 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 853 | if (hasPrimaryOutput()) { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 854 | if (state == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 855 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 856 | } else { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 857 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 858 | // force routing command to audio hardware when ending call |
| 859 | // even if no device change is needed |
| 860 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 861 | rxDevices = mPrimaryOutput->devices(); |
| 862 | } |
| 863 | if (oldState == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 864 | disconnectTelephonyAudioSource(mCallRxSourceClient); |
| 865 | disconnectTelephonyAudioSource(mCallTxSourceClient); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 866 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 867 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 868 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 869 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 870 | |
| 871 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 872 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 873 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 874 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 875 | if (state != AUDIO_MODE_IN_CALL || (desc != mPrimaryOutput && !isTelephonyRxOrTx(desc))) { |
| 876 | bool forceRouting = !newDevices.isEmpty(); |
| 877 | setOutputDevices(desc, newDevices, forceRouting, 0 /*delayMs*/, nullptr, |
| 878 | true /*requiresMuteCheck*/, !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 882 | if (isStateInCall(state)) { |
| 883 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 884 | // force reevaluating accessibility routing when call starts |
| 885 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | // 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] | 889 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 890 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 893 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 894 | return mEngine->getPhoneState(); |
| 895 | } |
| 896 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 897 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 898 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 899 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 900 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 901 | if (config == mEngine->getForceUse(usage)) { |
| 902 | return; |
| 903 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 904 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 905 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 906 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 907 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 908 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 909 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 910 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 911 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 912 | |
| 913 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 914 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 915 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 916 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 917 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
| 918 | mpClientInterface->invalidateStream(AUDIO_STREAM_SYSTEM); |
| 919 | mpClientInterface->invalidateStream(AUDIO_STREAM_ENFORCED_AUDIBLE); |
| 920 | } |
| 921 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 922 | //FIXME: workaround for truncated touch sounds |
| 923 | // to be removed when the problem is handled by system UI |
| 924 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 925 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 926 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 927 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 928 | |
| 929 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 930 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 933 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 934 | { |
| 935 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 936 | } |
| 937 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 938 | // Find an MSD output profile compatible with the parameters passed. |
| 939 | // When "directOnly" is set, restrict search to profiles for direct outputs. |
| 940 | sp<IOProfile> AudioPolicyManager::getMsdProfileForOutput( |
| 941 | const DeviceVector& devices, |
| 942 | uint32_t samplingRate, |
| 943 | audio_format_t format, |
| 944 | audio_channel_mask_t channelMask, |
| 945 | audio_output_flags_t flags, |
| 946 | bool directOnly) |
| 947 | { |
| 948 | flags = getRelevantFlags(flags, directOnly); |
| 949 | |
| 950 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 951 | if (msdModule != nullptr) { |
| 952 | // for the msd module check if there are patches to the output devices |
| 953 | if (msdHasPatchesToAllDevices(devices.toTypeAddrVector())) { |
| 954 | HwModuleCollection modules; |
| 955 | modules.add(msdModule); |
| 956 | return searchCompatibleProfileHwModules( |
| 957 | modules, getMsdAudioOutDevices(), samplingRate, format, channelMask, |
| 958 | flags, directOnly); |
| 959 | } |
| 960 | } |
| 961 | return nullptr; |
| 962 | } |
| 963 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 964 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 965 | // search to profiles for direct outputs. |
| 966 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 967 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 968 | uint32_t samplingRate, |
| 969 | audio_format_t format, |
| 970 | audio_channel_mask_t channelMask, |
| 971 | audio_output_flags_t flags, |
| 972 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 973 | { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 974 | flags = getRelevantFlags(flags, directOnly); |
| 975 | |
| 976 | return searchCompatibleProfileHwModules( |
| 977 | mHwModules, devices, samplingRate, format, channelMask, flags, directOnly); |
| 978 | } |
| 979 | |
| 980 | audio_output_flags_t AudioPolicyManager::getRelevantFlags ( |
| 981 | audio_output_flags_t flags, bool directOnly) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 982 | if (directOnly) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 983 | // only retain flags that will drive the direct output profile selection |
| 984 | // if explicitly requested |
| 985 | static const uint32_t kRelevantFlags = |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 986 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 987 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 988 | flags = (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 989 | } |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 990 | return flags; |
| 991 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 992 | |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 993 | sp<IOProfile> AudioPolicyManager::searchCompatibleProfileHwModules ( |
| 994 | const HwModuleCollection& hwModules, |
| 995 | const DeviceVector& devices, |
| 996 | uint32_t samplingRate, |
| 997 | audio_format_t format, |
| 998 | audio_channel_mask_t channelMask, |
| 999 | audio_output_flags_t flags, |
| 1000 | bool directOnly) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1001 | sp<IOProfile> profile; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1002 | for (const auto& hwModule : hwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 1003 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1004 | if (!curProfile->isCompatibleProfile(devices, |
| 1005 | samplingRate, NULL /*updatedSamplingRate*/, |
| 1006 | format, NULL /*updatedFormat*/, |
| 1007 | channelMask, NULL /*updatedChannelMask*/, |
| 1008 | flags)) { |
| 1009 | continue; |
| 1010 | } |
| 1011 | // reject profiles not corresponding to a device currently available |
| 1012 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 1013 | continue; |
| 1014 | } |
| 1015 | // reject profiles if connected device does not support codec |
| 1016 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
| 1017 | continue; |
| 1018 | } |
| 1019 | if (!directOnly) { |
| 1020 | return curProfile; |
| 1021 | } |
| 1022 | |
| 1023 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 1024 | // to one with offload capability |
| 1025 | if (profile != 0 && |
| 1026 | ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1027 | continue; |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 1028 | } |
| 1029 | profile = curProfile; |
| 1030 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1031 | break; |
| 1032 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1035 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1038 | sp<IOProfile> AudioPolicyManager::getSpatializerOutputProfile( |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 1039 | const audio_config_t *config __unused, const AudioDeviceTypeAddrVector &devices) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1040 | { |
| 1041 | for (const auto& hwModule : mHwModules) { |
| 1042 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 1c5e2e3 | 2021-08-18 18:50:28 +0200 | [diff] [blame] | 1043 | if (curProfile->getFlags() != AUDIO_OUTPUT_FLAG_SPATIALIZER) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1044 | continue; |
| 1045 | } |
| 1046 | // reject profiles not corresponding to a device currently available |
| 1047 | DeviceVector supportedDevices = curProfile->getSupportedDevices(); |
| 1048 | if (!mAvailableOutputDevices.containsAtLeastOne(supportedDevices)) { |
| 1049 | continue; |
| 1050 | } |
| 1051 | if (!devices.empty()) { |
| 1052 | if (supportedDevices.getDevicesFromDeviceTypeAddrVec(devices).size() |
| 1053 | != devices.size()) { |
| 1054 | continue; |
| 1055 | } |
| 1056 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1057 | ALOGV("%s found profile %s", __func__, curProfile->getName().c_str()); |
| 1058 | return curProfile; |
| 1059 | } |
| 1060 | } |
| 1061 | return nullptr; |
| 1062 | } |
| 1063 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1064 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1065 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1066 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1067 | |
| 1068 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 1069 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 1070 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 1071 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 1072 | // and AudioSystem::getOutputSamplingRate(). |
| 1073 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1074 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1075 | const audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1076 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1077 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 1078 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1079 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1082 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 1083 | const audio_attributes_t *srcAttr, |
| 1084 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1085 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1086 | if (srcAttr != NULL) { |
| 1087 | if (!isValidAttributes(srcAttr)) { |
| 1088 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 1089 | __func__, |
| 1090 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 1091 | srcAttr->tags); |
| 1092 | return BAD_VALUE; |
| 1093 | } |
| 1094 | *dstAttr = *srcAttr; |
| 1095 | } else { |
| 1096 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 1097 | ALOGE("%s: invalid stream type", __func__); |
| 1098 | return BAD_VALUE; |
| 1099 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1100 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1101 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1102 | |
| 1103 | // Only honor audibility enforced when required. The client will be |
| 1104 | // forced to reconnect if the forced usage changes. |
| 1105 | 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] | 1106 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 1107 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1110 | return NO_ERROR; |
| 1111 | } |
| 1112 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1113 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 1114 | audio_attributes_t *resultAttr, |
| 1115 | audio_io_handle_t *output, |
| 1116 | audio_session_t session, |
| 1117 | const audio_attributes_t *attr, |
| 1118 | audio_stream_type_t *stream, |
| 1119 | uid_t uid, |
| 1120 | const audio_config_t *config, |
| 1121 | audio_output_flags_t *flags, |
| 1122 | audio_port_handle_t *selectedDeviceId, |
| 1123 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1124 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1125 | output_type_t *outputType) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1126 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1127 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1128 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1129 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1130 | const sp<DeviceDescriptor> requestedDevice = |
| 1131 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1132 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1133 | *outputType = API_OUTPUT_INVALID; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1134 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 1135 | if (status != NO_ERROR) { |
| 1136 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1137 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1138 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1139 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1140 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1141 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1142 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1143 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1144 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1145 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1146 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1147 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1148 | // 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] | 1149 | sp<AudioPolicyMix> primaryMix; |
| 1150 | status = mPolicyMixes.getOutputForAttr(*resultAttr, uid, *flags, primaryMix, secondaryMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1151 | if (status != OK) { |
| 1152 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1153 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1154 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1155 | // Explicit routing is higher priority then any dynamic policy primary output |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1156 | bool usePrimaryOutputFromPolicyMixes = requestedDevice == nullptr && primaryMix != nullptr; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1157 | |
| 1158 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1159 | if ((usePrimaryOutputFromPolicyMixes |
| 1160 | || (secondaryMixes != nullptr && !secondaryMixes->empty())) |
Kevin Rocard | c2afbdf | 2019-01-31 18:18:06 -0800 | [diff] [blame] | 1161 | && !audio_is_linear_pcm(config->format)) { |
| 1162 | ALOGD("%s: rejecting request as dynamic audio policy only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1163 | return BAD_VALUE; |
| 1164 | } |
| 1165 | if (usePrimaryOutputFromPolicyMixes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1166 | sp<DeviceDescriptor> deviceDesc = |
| 1167 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1168 | primaryMix->mDeviceAddress, |
| 1169 | AUDIO_FORMAT_DEFAULT); |
| 1170 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Eric Laurent | c64e0ab | 2020-04-30 15:59:34 -0700 | [diff] [blame] | 1171 | if (deviceDesc != nullptr |
| 1172 | && (policyDesc == nullptr || (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT))) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1173 | audio_io_handle_t newOutput; |
| 1174 | status = openDirectOutput( |
| 1175 | *stream, session, config, |
| 1176 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
| 1177 | DeviceVector(deviceDesc), &newOutput); |
| 1178 | if (status != NO_ERROR) { |
| 1179 | policyDesc = nullptr; |
| 1180 | } else { |
| 1181 | policyDesc = mOutputs.valueFor(newOutput); |
| 1182 | primaryMix->setOutput(policyDesc); |
| 1183 | } |
| 1184 | } |
| 1185 | if (policyDesc != nullptr) { |
| 1186 | policyDesc->mPolicyMix = primaryMix; |
| 1187 | *output = policyDesc->mIoHandle; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1188 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1189 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1190 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1191 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1192 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1193 | } else { |
| 1194 | *outputType = API_OUTPUT_LEGACY; |
| 1195 | } |
| 1196 | return NO_ERROR; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1197 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1198 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1199 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1200 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1201 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1202 | return BAD_VALUE; |
| 1203 | } |
| 1204 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1205 | // in order to let the choice of the order to future vendor engine |
| 1206 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1207 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1208 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1209 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1212 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1213 | // chosen by the engine if not. |
| 1214 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1215 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1216 | // 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] | 1217 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1218 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1219 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1220 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1221 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1222 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1223 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1224 | } |
| 1225 | } |
| 1226 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1227 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1228 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1229 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1230 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1231 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1232 | if (!msdDevices.isEmpty()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1233 | *output = getOutputForDevices(msdDevices, session, resultAttr, config, flags); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1234 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1235 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1236 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1237 | } else { |
| 1238 | *output = AUDIO_IO_HANDLE_NONE; |
| 1239 | } |
| 1240 | } |
| 1241 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1242 | *output = getOutputForDevices(outputDevices, session, resultAttr, config, |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 1243 | flags, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1244 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1245 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 1246 | return INVALID_OPERATION; |
| 1247 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1248 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1249 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1250 | for (auto &outputDevice : outputDevices) { |
| 1251 | if (outputDevice->getId() == getConfig().getDefaultOutputDevice()->getId()) { |
| 1252 | *selectedDeviceId = outputDevice->getId(); |
| 1253 | break; |
| 1254 | } |
| 1255 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1256 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1257 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1258 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1259 | } else { |
| 1260 | *outputType = API_OUTPUT_LEGACY; |
| 1261 | } |
| 1262 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1263 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1264 | |
| 1265 | return NO_ERROR; |
| 1266 | } |
| 1267 | |
| 1268 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1269 | audio_io_handle_t *output, |
| 1270 | audio_session_t session, |
| 1271 | audio_stream_type_t *stream, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1272 | const AttributionSourceState& attributionSource, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1273 | const audio_config_t *config, |
| 1274 | audio_output_flags_t *flags, |
| 1275 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1276 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1277 | std::vector<audio_io_handle_t> *secondaryOutputs, |
| 1278 | output_type_t *outputType) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1279 | { |
| 1280 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1281 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1282 | return INVALID_OPERATION; |
| 1283 | } |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1284 | const uid_t uid = VALUE_OR_RETURN_STATUS( |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1285 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1286 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1287 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1288 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1289 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1290 | const sp<DeviceDescriptor> requestedDevice = |
| 1291 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1292 | |
| 1293 | // Prevent from storing invalid requested device id in clients |
| 1294 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1295 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1296 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1297 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1298 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1299 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1300 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1301 | if (status != NO_ERROR) { |
| 1302 | return status; |
| 1303 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1304 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1305 | if (secondaryOutputs != nullptr) { |
| 1306 | for (auto &secondaryMix : secondaryMixes) { |
| 1307 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1308 | if (outputDesc != nullptr && |
| 1309 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1310 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1311 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1312 | } |
| 1313 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1314 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1315 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1316 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1317 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1318 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1319 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1320 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1321 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1322 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1323 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1324 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1325 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1326 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1327 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1328 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1329 | std::move(weakSecondaryOutputDescs), |
| 1330 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1331 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1332 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1333 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1334 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1335 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1336 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1337 | } |
| 1338 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1339 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1340 | audio_session_t session, |
| 1341 | const audio_config_t *config, |
| 1342 | audio_output_flags_t flags, |
| 1343 | const DeviceVector &devices, |
| 1344 | audio_io_handle_t *output) { |
| 1345 | |
| 1346 | *output = AUDIO_IO_HANDLE_NONE; |
| 1347 | |
| 1348 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1349 | // and not explicitly requested |
| 1350 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1351 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1352 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1353 | return NAME_NOT_FOUND; |
| 1354 | } |
| 1355 | |
| 1356 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1357 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1358 | // when audioflinger detects there is an active non offloadable effect. |
| 1359 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1360 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1361 | // in the background. |
| 1362 | sp<IOProfile> profile; |
| 1363 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1364 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1365 | profile = getProfileForOutput( |
| 1366 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1367 | flags, true /* directOnly */); |
| 1368 | } |
| 1369 | |
| 1370 | if (profile == nullptr) { |
| 1371 | return NAME_NOT_FOUND; |
| 1372 | } |
| 1373 | |
| 1374 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1375 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1376 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1377 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1378 | // reuse direct output if currently open by the same client |
| 1379 | // and configured with same parameters |
| 1380 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1381 | (config->format == desc->getFormat()) && |
| 1382 | (config->channel_mask == desc->getChannelMask()) && |
| 1383 | (session == desc->mDirectClientSession)) { |
| 1384 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1385 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1386 | mOutputs.keyAt(i), session); |
| 1387 | *output = mOutputs.keyAt(i); |
| 1388 | return NO_ERROR; |
| 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | if (!profile->canOpenNewIo()) { |
| 1394 | return NAME_NOT_FOUND; |
| 1395 | } |
| 1396 | |
| 1397 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1398 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 1399 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1400 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1401 | // 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] | 1402 | releaseMsdOutputPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1403 | |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 1404 | status_t status = |
| 1405 | outputDesc->open(config, nullptr /* mixerConfig */, devices, stream, flags, output); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1406 | |
| 1407 | // only accept an output with the requested parameters |
| 1408 | if (status != NO_ERROR || |
| 1409 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1410 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1411 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1412 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1413 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1414 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1415 | config->channel_mask, outputDesc->getChannelMask()); |
| 1416 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1417 | outputDesc->close(); |
| 1418 | } |
| 1419 | // fall back to mixer output if possible when the direct output could not be open |
| 1420 | if (audio_is_linear_pcm(config->format) && |
| 1421 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1422 | return NAME_NOT_FOUND; |
| 1423 | } |
| 1424 | *output = AUDIO_IO_HANDLE_NONE; |
| 1425 | return BAD_VALUE; |
| 1426 | } |
| 1427 | outputDesc->mDirectOpenCount = 1; |
| 1428 | outputDesc->mDirectClientSession = session; |
| 1429 | |
| 1430 | addOutput(*output, outputDesc); |
| 1431 | mPreviousOutputs = mOutputs; |
| 1432 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1433 | mpClientInterface->onAudioPortListUpdate(); |
| 1434 | return NO_ERROR; |
| 1435 | } |
| 1436 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1437 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1438 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1439 | audio_session_t session, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1440 | const audio_attributes_t *attr, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1441 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1442 | audio_output_flags_t *flags, |
| 1443 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1444 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1445 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1446 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1447 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1448 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1449 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1450 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1451 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1452 | // open a direct output if required by specified parameters |
| 1453 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1454 | // and all common behaviors are driven by checking only the direct flag |
| 1455 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1456 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1457 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1458 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1459 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1460 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1461 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1462 | |
| 1463 | audio_stream_type_t stream = mEngine->getStreamTypeForAttributes(*attr); |
| 1464 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1465 | // only allow deep buffering for music stream type |
| 1466 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1467 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1468 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1469 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1470 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1471 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1472 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1473 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1474 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1475 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1476 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1477 | audio_is_linear_pcm(config->format) && |
| 1478 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1479 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1480 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1481 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1482 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1483 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 1484 | // Attach the Ultrasound flag for the AUDIO_CONTENT_TYPE_ULTRASOUND |
| 1485 | if (attr->content_type == AUDIO_CONTENT_TYPE_ULTRASOUND) { |
| 1486 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_ULTRASOUND); |
| 1487 | } |
| 1488 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1489 | if (mSpatializerOutput != nullptr |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 1490 | && canBeSpatializedInt(attr, config, |
| 1491 | devices.toTypeAddrVector(), false /* allowCurrentOutputReconfig */)) { |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 1492 | return mSpatializerOutput->mIoHandle; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 1493 | } |
| 1494 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1495 | audio_config_t directConfig = *config; |
| 1496 | directConfig.channel_mask = channelMask; |
| 1497 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1498 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1499 | return output; |
| 1500 | } |
| 1501 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1502 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1503 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1504 | 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] | 1505 | return AUDIO_IO_HANDLE_NONE; |
| 1506 | } |
| 1507 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1508 | // ignoring channel mask due to downmix capability in mixer |
| 1509 | |
| 1510 | // open a non direct output |
| 1511 | |
| 1512 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1513 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1514 | // get which output is suitable for the specified stream. The actual |
| 1515 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1516 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1517 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1518 | // 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] | 1519 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1520 | output = selectOutput( |
| 1521 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1522 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1523 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1524 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1525 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1526 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1527 | return output; |
| 1528 | } |
| 1529 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1530 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1531 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1532 | mAvailableInputDevices); |
| 1533 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1534 | } |
| 1535 | |
| 1536 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1537 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1538 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1539 | } |
| 1540 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1541 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1542 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1543 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1544 | if (msdModule != 0) { |
| 1545 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1546 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1547 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1548 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1549 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1550 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1551 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1552 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | return msdPatches; |
| 1557 | } |
| 1558 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 1559 | bool AudioPolicyManager::isMsdPatch(const audio_patch_handle_t &handle) const { |
| 1560 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 1561 | if (index < 0) { |
| 1562 | return false; |
| 1563 | } |
| 1564 | const sp<AudioPatch> patch = mAudioPatches.valueAt(index); |
| 1565 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1566 | if (msdModule == nullptr) { |
| 1567 | return false; |
| 1568 | } |
| 1569 | const struct audio_port_config *sink = &patch->mPatch.sinks[0]; |
| 1570 | if (getMsdAudioOutDevices().contains(mAvailableOutputDevices.getDeviceFromId(sink->id))) { |
| 1571 | return true; |
| 1572 | } |
| 1573 | index = getMsdOutputPatches().indexOfKey(handle); |
| 1574 | if (index < 0) { |
| 1575 | return false; |
| 1576 | } |
| 1577 | return true; |
| 1578 | } |
| 1579 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1580 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, |
| 1581 | const InputProfileCollection &inputProfiles, |
| 1582 | const OutputProfileCollection &outputProfiles, |
| 1583 | const sp<DeviceDescriptor> &sourceDevice, |
| 1584 | const sp<DeviceDescriptor> &sinkDevice, |
| 1585 | AudioProfileVector& sourceProfiles, |
| 1586 | AudioProfileVector& sinkProfiles) const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1587 | if (inputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1588 | ALOGE("%s() no input profiles for source module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1589 | return NO_INIT; |
| 1590 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1591 | if (outputProfiles.isEmpty()) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1592 | ALOGE("%s() no output profiles for sink module", __func__); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1593 | return NO_INIT; |
| 1594 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1595 | for (const auto &inProfile : inputProfiles) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1596 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1597 | inProfile->supportsDevice(sourceDevice)) { |
| 1598 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1599 | } |
| 1600 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1601 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1602 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1603 | outProfile->supportsDevice(sinkDevice)) { |
| 1604 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1605 | } |
| 1606 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1607 | return NO_ERROR; |
| 1608 | } |
| 1609 | |
| 1610 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, |
| 1611 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, |
| 1612 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1613 | { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1614 | struct audio_config_base bestSinkConfig; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1615 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, |
| 1616 | msdCompressedFormatsOrder, msdSurroundChannelMasksOrder, |
| 1617 | true /*preferHigherSamplingRates*/, bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1618 | if (result != NO_ERROR) { |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1619 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", |
| 1620 | __func__, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1621 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1622 | } |
| 1623 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1624 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1625 | sinkConfig->format = bestSinkConfig.format; |
| 1626 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1627 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1628 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1629 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1630 | // For formats compatible with IEC61937 encapsulation, assume that |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1631 | // the input is IEC61937 framed (for proportional buffer sizing). |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1632 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1633 | // raw and IEC61937 framed streams. |
| 1634 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1635 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1636 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1637 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1638 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1639 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1640 | sourceConfig->format = bestSinkConfig.format; |
| 1641 | // Copy input stream directly without any processing (e.g. resampling). |
| 1642 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1643 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1644 | if (hwAvSync) { |
| 1645 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1646 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1647 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1648 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1649 | } |
| 1650 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1651 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1652 | sinkConfig->config_mask |= config_mask; |
| 1653 | sourceConfig->config_mask |= config_mask; |
| 1654 | return NO_ERROR; |
| 1655 | } |
| 1656 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1657 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, |
| 1658 | const sp<DeviceDescriptor> &device) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1659 | { |
| 1660 | PatchBuilder patchBuilder; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1661 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1662 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); |
| 1663 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 1664 | if (deviceModule == nullptr) { |
| 1665 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); |
| 1666 | return patchBuilder; |
| 1667 | } |
| 1668 | const InputProfileCollection inputProfiles = msdIsSource ? |
| 1669 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); |
| 1670 | const OutputProfileCollection outputProfiles = msdIsSource ? |
| 1671 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); |
| 1672 | |
| 1673 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; |
| 1674 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? |
| 1675 | device : getMsdAudioOutDevices().itemAt(0); |
| 1676 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); |
| 1677 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1678 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1679 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1680 | AudioProfileVector sourceProfiles; |
| 1681 | AudioProfileVector sinkProfiles; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1682 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1683 | // For now, we just forcefully try with HwAvSync first. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1684 | for (auto hwAvSync : { true, false }) { |
| 1685 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, |
| 1686 | sourceProfiles, sinkProfiles) != NO_ERROR) { |
| 1687 | continue; |
| 1688 | } |
| 1689 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, |
| 1690 | &sinkConfig) == NO_ERROR) { |
| 1691 | // Found a matching config. Re-create PatchBuilder with this config. |
| 1692 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1693 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1694 | } |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1695 | ALOGV("%s() no matching config found. Fall through to default PCM patch" |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1696 | " supporting PCM format conversion.", __func__); |
| 1697 | return patchBuilder; |
| 1698 | } |
| 1699 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1700 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1701 | DeviceVector devices; |
| 1702 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1703 | devices.add(*outputDevices); |
| 1704 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1705 | // Use media strategy for unspecified output device. This should only |
| 1706 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1707 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1708 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1709 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1710 | 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] | 1711 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1712 | std::vector<PatchBuilder> patchesToCreate; |
| 1713 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1714 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1715 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1716 | } |
| 1717 | // Retain only the MSD patches associated with outputDevices request. |
| 1718 | // Tear down the others, and create new ones as needed. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1719 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1720 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1721 | auto retainedPatch = false; |
| 1722 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1723 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1724 | patchesToRemove.removeItemsAt(i); |
| 1725 | retainedPatch = true; |
| 1726 | break; |
| 1727 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1728 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1729 | if (retainedPatch) { |
| 1730 | it = patchesToCreate.erase(it); |
| 1731 | continue; |
| 1732 | } |
| 1733 | ++it; |
| 1734 | } |
| 1735 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1736 | return NO_ERROR; |
| 1737 | } |
| 1738 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1739 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1740 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1741 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1742 | status_t status = NO_ERROR; |
| 1743 | for (const auto &p : patchesToCreate) { |
| 1744 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1745 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1746 | char message[256]; |
| 1747 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1748 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1749 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1750 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1751 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1752 | if (currStatus == NO_ERROR) { |
| 1753 | ALOGD("%s", message); |
| 1754 | } else { |
| 1755 | ALOGE("%s", message); |
| 1756 | if (status == NO_ERROR) { |
| 1757 | status = currStatus; |
| 1758 | } |
| 1759 | } |
| 1760 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1761 | return status; |
| 1762 | } |
| 1763 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1764 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { |
| 1765 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1766 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1767 | const auto& patch = msdPatches[i]; |
| 1768 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1769 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1770 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1771 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1772 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1773 | break; |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 1779 | bool AudioPolicyManager::msdHasPatchesToAllDevices(const AudioDeviceTypeAddrVector& devices) { |
| 1780 | DeviceVector devicesToCheck = mOutputDevicesAll.getDevicesFromDeviceTypeAddrVec(devices); |
| 1781 | AudioPatchCollection msdPatches = getMsdOutputPatches(); |
| 1782 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1783 | const auto& patch = msdPatches[i]; |
| 1784 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1785 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1786 | if (sink->type == AUDIO_PORT_TYPE_DEVICE) { |
| 1787 | const auto& foundDevice = devicesToCheck.getDevice( |
| 1788 | sink->ext.device.type, String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT); |
| 1789 | if (foundDevice != nullptr) { |
| 1790 | devicesToCheck.remove(foundDevice); |
| 1791 | if (devicesToCheck.isEmpty()) { |
| 1792 | return true; |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | } |
| 1797 | } |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1801 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1802 | audio_output_flags_t flags, |
| 1803 | audio_format_t format, |
| 1804 | audio_channel_mask_t channelMask, |
| 1805 | uint32_t samplingRate, |
| 1806 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1807 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1808 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 1809 | "%s called with format %#x", __func__, format); |
| 1810 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1811 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 1812 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 1813 | // haptic-generating effect attached to is in given outputs. |
| 1814 | if (sessionId != AUDIO_SESSION_NONE) { |
| 1815 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 1816 | sessionId, FX_IID_HAPTICGENERATOR); |
| 1817 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 1818 | return hapticGeneratingOutput; |
| 1819 | } |
| 1820 | } |
| 1821 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1822 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 1823 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 1824 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1825 | |
| 1826 | // Flags expressing a functional request: must be honored in priority over |
| 1827 | // other criteria |
| 1828 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 1829 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 1830 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM | AUDIO_OUTPUT_FLAG_ULTRASOUND | |
| 1831 | AUDIO_OUTPUT_FLAG_SPATIALIZER); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1832 | // Flags expressing a performance request: have lower priority than serving |
| 1833 | // requested sampling rate or channel mask |
| 1834 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 1835 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 1836 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 1837 | |
| 1838 | const audio_output_flags_t functionalFlags = |
| 1839 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 1840 | const audio_output_flags_t performanceFlags = |
| 1841 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 1842 | |
| 1843 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 1844 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1845 | // 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] | 1846 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1847 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1848 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1849 | // 2: the output with the highest number of requested functional flags |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1850 | // with tiebreak preferring the minimum number of extra functional flags |
| 1851 | // (see b/200293124, the incorrect selection of AUDIO_OUTPUT_FLAG_VOIP_RX). |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1852 | // 3: the output supporting the exact channel mask |
| 1853 | // 4: the output with a higher channel count than requested |
| 1854 | // 5: the output with a higher sampling rate than requested |
| 1855 | // 6: the output with the highest number of requested performance flags |
| 1856 | // 7: the output with the bit depth the closest to the requested one |
| 1857 | // 8: the primary output |
| 1858 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1859 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1860 | // matching criteria values in priority order for best matching output so far |
| 1861 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1862 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1863 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 1864 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1865 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1866 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1867 | for (audio_io_handle_t output : outputs) { |
| 1868 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1869 | // matching criteria values in priority order for current output |
| 1870 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1871 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1872 | if (outputDesc->isDuplicated()) { |
| 1873 | continue; |
| 1874 | } |
| 1875 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 1876 | continue; |
| 1877 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1878 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1879 | // If haptic channel is specified, use the haptic output if present. |
| 1880 | // When using haptic output, same audio format and sample rate are required. |
| 1881 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1882 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1883 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 1884 | continue; |
| 1885 | } |
| 1886 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1887 | && format == outputDesc->getFormat() |
| 1888 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1889 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 1890 | } |
| 1891 | |
| 1892 | // functional flags match |
Carter Hsu | 199f189 | 2021-10-15 15:47:29 +0800 | [diff] [blame] | 1893 | const int matchingFunctionalFlags = |
| 1894 | __builtin_popcount(outputDesc->mFlags & functionalFlags); |
| 1895 | const int totalFunctionalFlags = |
| 1896 | __builtin_popcount(outputDesc->mFlags & kFunctionalFlags); |
| 1897 | // Prefer matching functional flags, but subtract unnecessary functional flags. |
| 1898 | currentMatchCriteria[1] = 100 * (matchingFunctionalFlags + 1) - totalFunctionalFlags; |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1899 | |
| 1900 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1901 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 1902 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1903 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 1904 | channelCount <= outputChannelCount) { |
| 1905 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1906 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 1907 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1908 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1909 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1910 | currentMatchCriteria[3] = outputChannelCount; |
| 1911 | } |
| 1912 | |
| 1913 | // sampling rate match |
| 1914 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT && |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1915 | samplingRate <= outputDesc->getSamplingRate()) { |
| 1916 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | // performance flags match |
| 1920 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 1921 | |
| 1922 | // format match |
| 1923 | if (format != AUDIO_FORMAT_INVALID) { |
| 1924 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1925 | PolicyAudioPort::kFormatDistanceMax - |
| 1926 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | // primary output match |
| 1930 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 1931 | |
| 1932 | // compare match criteria by priority then value |
| 1933 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 1934 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 1935 | bestMatchCriteria = currentMatchCriteria; |
| 1936 | bestOutput = output; |
| 1937 | |
| 1938 | std::stringstream result; |
| 1939 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 1940 | std::ostream_iterator<int>(result, " ")); |
| 1941 | ALOGV("%s new bestOutput %d criteria %s", |
| 1942 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1943 | } |
| 1944 | } |
| 1945 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1946 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1949 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1950 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1951 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1952 | |
| 1953 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1954 | if (outputDesc == 0) { |
| 1955 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1956 | return BAD_VALUE; |
| 1957 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1958 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1959 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1960 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1961 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1962 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1963 | status_t status = outputDesc->start(); |
| 1964 | if (status != NO_ERROR) { |
| 1965 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1966 | } |
| 1967 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1968 | uint32_t delayMs; |
| 1969 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1970 | |
| 1971 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1972 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1973 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1974 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1975 | if (delayMs != 0) { |
| 1976 | usleep(delayMs * 1000); |
| 1977 | } |
| 1978 | |
| 1979 | return status; |
| 1980 | } |
| 1981 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1982 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1983 | const sp<TrackClientDescriptor>& client, |
| 1984 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1985 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1986 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1987 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1988 | |
| 1989 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1990 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1991 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1992 | auto clientStrategy = client->strategy(); |
| 1993 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1994 | if (stream == AUDIO_STREAM_TTS) { |
| 1995 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1996 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 1997 | toVolumeSource(AUDIO_STREAM_TTS, false) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1998 | return INVALID_OPERATION; |
| 1999 | } else { |
| 2000 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 2001 | } |
| 2002 | } else { |
| 2003 | // some playback other than beacon starts |
| 2004 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 2005 | } |
| 2006 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2007 | // 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] | 2008 | // check active before incrementing usage count |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 2009 | bool force = !outputDesc->isActive() && !outputDesc->isRouted(); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 2010 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2011 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2012 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2013 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2014 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2015 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2016 | address = policyMix->mDeviceAddress.string(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2017 | 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] | 2018 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2019 | } else { |
| 2020 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2021 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 2022 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 2023 | AUDIO_FORMAT_DEFAULT); |
| 2024 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 2025 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2028 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 2029 | // It covers a common case when there is no materially active audio |
| 2030 | // and muting would result in unnecessary delay and dropped audio. |
| 2031 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 2032 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 2033 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2034 | // increment usage count for this stream on the requested output: |
| 2035 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 2036 | // 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] | 2037 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2038 | |
| 2039 | if (client->hasPreferredDevice(true)) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 2040 | if (outputDesc->clientsList(true /*activeOnly*/).size() == 1 && |
| 2041 | client->isPreferredDeviceForExclusiveUse()) { |
| 2042 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 2043 | devices = DeviceVector( |
| 2044 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 2045 | } else { |
| 2046 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 2047 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2048 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2049 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2050 | } |
| 2051 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2052 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2053 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2054 | selectOutputForMusicEffects(); |
| 2055 | } |
| 2056 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2057 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2058 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2059 | if (devices.isEmpty()) { |
| 2060 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2061 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2062 | bool shouldWait = |
| 2063 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 2064 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 2065 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2066 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2067 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2068 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2069 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2070 | // An output has a shared device if |
| 2071 | // - managed by the same hw module |
| 2072 | // - supports the currently selected device |
| 2073 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2074 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2075 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2076 | // force a device change if any other output is: |
| 2077 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 2078 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2079 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2080 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2081 | // 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] | 2082 | // change the device currently selected by the other output. |
| 2083 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2084 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 2085 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2086 | force = true; |
| 2087 | } |
| 2088 | // 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] | 2089 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 2090 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2091 | const uint32_t latencyMs = desc->latency(); |
| 2092 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 2093 | |
| 2094 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 2095 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2096 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2097 | |
| 2098 | // Require mute check if another output is on a shared device |
| 2099 | // and currently active to have proper drain and avoid pops. |
| 2100 | // Note restoring AudioTracks onto this output needs to invoke |
| 2101 | // a volume ramp if there is no mute. |
| 2102 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2103 | } |
| 2104 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2105 | |
| 2106 | const uint32_t muteWaitMs = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2107 | setOutputDevices(outputDesc, devices, force, 0, NULL, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2108 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2109 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2110 | auto &curves = getVolumeCurves(client->attributes()); |
| 2111 | checkAndSetVolume(curves, client->volumeSource(), |
| 2112 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2113 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 2114 | outputDesc->devices().types(), 0 /*delay*/, |
| 2115 | outputDesc->useHwGain() /*force*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2116 | |
| 2117 | // update the outputs if starting an output with a stream that can affect notification |
| 2118 | // routing |
| 2119 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2120 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2121 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2122 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 2123 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 2124 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2125 | |
| 2126 | if (waitMs > muteWaitMs) { |
| 2127 | *delayMs = waitMs - muteWaitMs; |
| 2128 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 2129 | |
| 2130 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 2131 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 2132 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 2133 | // change occurs after the MixerThread starts and causes a stream volume |
| 2134 | // glitch. |
| 2135 | // |
| 2136 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2137 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2138 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2139 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2140 | mEngine->getForceUse( |
| 2141 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2142 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2143 | } |
| 2144 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2145 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 2146 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2147 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 2148 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2149 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2150 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2151 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2152 | "remote-submix", |
| 2153 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2156 | return NO_ERROR; |
| 2157 | } |
| 2158 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2159 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2160 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2161 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2162 | |
| 2163 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2164 | if (outputDesc == 0) { |
| 2165 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2166 | return BAD_VALUE; |
| 2167 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2168 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2169 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2170 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 2171 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2172 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2173 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2174 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2175 | if (status == NO_ERROR ) { |
| 2176 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2177 | } |
| 2178 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2179 | } |
| 2180 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2181 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 2182 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2183 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2184 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2185 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2186 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2187 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2188 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 2189 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2190 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 2191 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2192 | // Automatically disable the remote submix input when output is stopped on a |
| 2193 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2194 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2195 | if (isSingleDeviceType( |
| 2196 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2197 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2198 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2199 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2200 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2201 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2202 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | bool forceDeviceUpdate = false; |
| 2206 | if (client->hasPreferredDevice(true)) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2207 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2208 | forceDeviceUpdate = true; |
| 2209 | } |
| 2210 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2211 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2212 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2213 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2214 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2215 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2216 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2217 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2218 | |
| 2219 | // If the routing does not change, if an output is routed on a device using HwGain |
| 2220 | // (aka setAudioPortConfig) and there are still active clients following different |
| 2221 | // volume group(s), force reapply volume |
| 2222 | bool requiresVolumeCheck = outputDesc->getActivityCount(clientVolSrc) == 0 && |
| 2223 | outputDesc->useHwGain() && outputDesc->isAnyActive(VOLUME_SOURCE_NONE); |
| 2224 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2225 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 2226 | // the track stop() command is received and at that time the audio track buffer can |
| 2227 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 2228 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 2229 | // audio path (audio DSP, CODEC ...) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 2230 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2, |
| 2231 | nullptr, true /*requiresMuteCheck*/, requiresVolumeCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2232 | |
| 2233 | // force restoring the device selection on other active outputs if it differs from the |
| 2234 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2235 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2236 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2237 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2238 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2239 | desc->isActive() && |
| 2240 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2241 | (newDevices != desc->devices())) { |
| 2242 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2243 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2244 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2245 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 2246 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2247 | // re-apply device specific volume if not done by setOutputDevice() |
| 2248 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2249 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2250 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2254 | handleNotificationRoutingForStream(stream); |
| 2255 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2256 | |
| 2257 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2258 | 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] | 2259 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2260 | } |
| 2261 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2262 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2263 | selectOutputForMusicEffects(); |
| 2264 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2265 | return NO_ERROR; |
| 2266 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2267 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2268 | return INVALID_OPERATION; |
| 2269 | } |
| 2270 | } |
| 2271 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2272 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2273 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2274 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2275 | |
| 2276 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2277 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2278 | // If an output descriptor is closed due to a device routing change, |
| 2279 | // then there are race conditions with releaseOutput from tracks |
| 2280 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2281 | // destroyed shortly thereafter. |
| 2282 | // |
| 2283 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2284 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2285 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2286 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2287 | |
| 2288 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2289 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2290 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2291 | if (outputDesc->isClientActive(client)) { |
| 2292 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2293 | stopOutput(portId); |
| 2294 | } |
| 2295 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2296 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2297 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2298 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2299 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2300 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2301 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2302 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2303 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2304 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2305 | } |
| 2306 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2307 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2308 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2309 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2310 | // The output is pending reopened to query dynamic profiles and |
| 2311 | // there is no active clients |
| 2312 | closeOutput(outputDesc->mIoHandle); |
| 2313 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2314 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2315 | if (newOutputDesc == nullptr) { |
| 2316 | ALOGE("%s failed to open output", __func__); |
| 2317 | } |
| 2318 | return true; |
| 2319 | } |
| 2320 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2323 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2324 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2325 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2326 | audio_session_t session, |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2327 | const AttributionSourceState& attributionSource, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2328 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2329 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2330 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2331 | input_type_t *inputType, |
| 2332 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2333 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2334 | 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] | 2335 | "flags %#x attributes=%s requested device ID %d", |
| 2336 | __func__, attr->source, config->sample_rate, config->format, config->channel_mask, |
| 2337 | session, flags, toString(*attr).c_str(), *selectedDeviceId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2338 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2339 | status_t status = NO_ERROR; |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 2340 | audio_source_t halInputSource; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2341 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2342 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2343 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2344 | sp<AudioInputDescriptor> inputDesc; |
| 2345 | sp<RecordClientDescriptor> clientDesc; |
| 2346 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Svet Ganov | 3e5f14f | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2347 | 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] | 2348 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2349 | |
| 2350 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2351 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2352 | return INVALID_OPERATION; |
| 2353 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2354 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2355 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2356 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2357 | } |
| 2358 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2359 | // Explicit routing? |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2360 | sp<DeviceDescriptor> explicitRoutingDevice = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2361 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2362 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2363 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2364 | // possible |
| 2365 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2366 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2367 | ssize_t index = mInputs.indexOfKey(*input); |
| 2368 | if (index < 0) { |
| 2369 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2370 | status = BAD_VALUE; |
| 2371 | goto error; |
| 2372 | } |
| 2373 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2374 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2375 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2376 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2377 | status = BAD_VALUE; |
| 2378 | goto error; |
| 2379 | } |
| 2380 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2381 | // 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] | 2382 | // corresponds to a new client and is only permitted from the same UID. |
| 2383 | // 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] | 2384 | if (clients.size() > 1) { |
| 2385 | for (const auto& client : clients) { |
| 2386 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2387 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2388 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2389 | // to an actual client |
| 2390 | if (client == *clients.cbegin()) { |
| 2391 | continue; |
| 2392 | } |
| 2393 | if (uid != client->uid() && !client->isSilenced()) { |
| 2394 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2395 | uid, client->portId(), client->uid()); |
| 2396 | status = INVALID_OPERATION; |
| 2397 | goto error; |
| 2398 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2399 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2400 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2401 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2402 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2403 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2404 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2405 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | *input = AUDIO_IO_HANDLE_NONE; |
| 2409 | *inputType = API_INPUT_INVALID; |
| 2410 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2411 | halInputSource = attributes.source; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2412 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2413 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
| 2414 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { |
| 2415 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2416 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2417 | ALOGW("%s could not find input mix for attr %s", |
| 2418 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2419 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2420 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2421 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2422 | String8(attr->tags + strlen("addr=")), |
| 2423 | AUDIO_FORMAT_DEFAULT); |
| 2424 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2425 | 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] | 2426 | __func__, attributes.source, attributes.tags); |
| 2427 | status = BAD_VALUE; |
| 2428 | goto error; |
| 2429 | } |
| 2430 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2431 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2432 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2433 | } else { |
| 2434 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2435 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2436 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2437 | if (explicitRoutingDevice != nullptr) { |
| 2438 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2439 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2440 | // Prevent from storing invalid requested device id in clients |
| 2441 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 2442 | device = mEngine->getInputDeviceForAttributes(attributes, uid, &policyMix); |
yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2443 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", |
| 2444 | __FUNCTION__, device->type()); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2445 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2446 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2447 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2448 | status = BAD_VALUE; |
| 2449 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2450 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2451 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2452 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2453 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2454 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2455 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2456 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2457 | // know about it and is therefore considered "legacy" |
| 2458 | *inputType = API_INPUT_LEGACY; |
| 2459 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2460 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2461 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2462 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2463 | } else { |
| 2464 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2465 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2466 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2467 | } |
| 2468 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2469 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2470 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2471 | status = INVALID_OPERATION; |
| 2472 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2473 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2474 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2475 | exit: |
| 2476 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2477 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2478 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2479 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2480 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2481 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2482 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2483 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2484 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2485 | requestedDeviceId, attributes.source, flags, |
| 2486 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2487 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2488 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2489 | |
| 2490 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2491 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2492 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2493 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2494 | |
| 2495 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2496 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2500 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2501 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2502 | const audio_attributes_t &attributes, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2503 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2504 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2505 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2506 | { |
| 2507 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2508 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2509 | bool isSoundTrigger = false; |
| 2510 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2511 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2512 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2513 | if (index >= 0) { |
| 2514 | input = mSoundTriggerSessions.valueFor(session); |
| 2515 | isSoundTrigger = true; |
| 2516 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2517 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2518 | } else { |
| 2519 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2520 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2521 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2522 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2523 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2524 | } |
| 2525 | |
Carter Hsu | a3abb40 | 2021-10-26 11:11:20 +0800 | [diff] [blame] | 2526 | if (attributes.source == AUDIO_SOURCE_ULTRASOUND) { |
| 2527 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_ULTRASOUND); |
| 2528 | } |
| 2529 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2530 | // find a compatible input profile (not necessarily identical in parameters) |
| 2531 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2532 | // sampling rate and flags may be updated by getInputProfile |
| 2533 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2534 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 2535 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2536 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2537 | audio_input_flags_t profileFlags = flags; |
| 2538 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 2539 | profileFormat = config->format; // reset each time through loop, in case it is updated |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2540 | profile = getInputProfile(device, profileSamplingRate, profileFormat, profileChannelMask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2541 | profileFlags); |
| 2542 | if (profile != 0) { |
| 2543 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2544 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 2545 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Atneya Nair | 497fff1 | 2022-01-18 16:23:04 -0500 | [diff] [blame] | 2546 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE && audio_is_linear_pcm(config->format)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2547 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 2548 | } else { // fail |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2549 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 2550 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2551 | config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2552 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2553 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2554 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2555 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2556 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2557 | if (samplingRate == 0) { |
| 2558 | samplingRate = profileSamplingRate; |
| 2559 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2560 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2561 | if (profile->getModuleHandle() == 0) { |
| 2562 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2563 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2566 | // Reuse an already opened input if a client with the same session ID already exists |
| 2567 | // on that input |
| 2568 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2569 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2570 | if (desc->mProfile != profile) { |
| 2571 | continue; |
| 2572 | } |
| 2573 | RecordClientVector clients = desc->clientsList(); |
| 2574 | for (const auto &client : clients) { |
| 2575 | if (session == client->session()) { |
| 2576 | return desc->mIoHandle; |
| 2577 | } |
| 2578 | } |
| 2579 | } |
| 2580 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2581 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2582 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2583 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2584 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2585 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2586 | continue; |
| 2587 | } |
| 2588 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2589 | // else |
| 2590 | // reuse input if active client app is not in IDLE state |
| 2591 | // |
| 2592 | RecordClientVector clients = desc->clientsList(); |
| 2593 | bool doClose = false; |
| 2594 | for (const auto& client : clients) { |
| 2595 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2596 | continue; |
| 2597 | } |
| 2598 | if (client->isSoundTrigger()) { |
| 2599 | if (session == client->session()) { |
| 2600 | return desc->mIoHandle; |
| 2601 | } |
| 2602 | continue; |
| 2603 | } |
| 2604 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2605 | return desc->mIoHandle; |
| 2606 | } |
| 2607 | doClose = true; |
| 2608 | } |
| 2609 | if (doClose) { |
| 2610 | closeInput(desc->mIoHandle); |
| 2611 | } else { |
| 2612 | i++; |
| 2613 | } |
| 2614 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2615 | } |
| 2616 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2617 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2618 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2619 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2620 | lConfig.sample_rate = profileSamplingRate; |
| 2621 | lConfig.channel_mask = profileChannelMask; |
| 2622 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2623 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2624 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2625 | |
| 2626 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2627 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2628 | (profileSamplingRate != lConfig.sample_rate) || |
| 2629 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2630 | (profileChannelMask != lConfig.channel_mask)) { |
| 2631 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2632 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2633 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2634 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2635 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2636 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2637 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2640 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2641 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2642 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2643 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2644 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2645 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2646 | } |
| 2647 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2648 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2649 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2650 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2651 | |
| 2652 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2653 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2654 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2655 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2656 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2657 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2658 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2659 | if (client->active()) { |
| 2660 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2661 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2662 | } |
| 2663 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2664 | audio_session_t session = client->session(); |
| 2665 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2666 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2667 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2668 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2669 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2670 | status_t status = inputDesc->start(); |
| 2671 | if (status != NO_ERROR) { |
| 2672 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2673 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2674 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2675 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2676 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2677 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2678 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2679 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2680 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2681 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2682 | if (device != nullptr) { |
| 2683 | status = setInputDevice(input, device, true /* force */); |
| 2684 | } else { |
| 2685 | ALOGW("%s no new input device can be found for descriptor %d", |
| 2686 | __FUNCTION__, inputDesc->getId()); |
| 2687 | status = BAD_VALUE; |
| 2688 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2689 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2690 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2691 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2692 | // 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] | 2693 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2694 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2695 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2696 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2697 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2698 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2699 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2700 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2701 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2702 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2703 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2704 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2705 | // automatically enable the remote submix output when input is started if not |
| 2706 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2707 | // 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] | 2708 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2709 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2710 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2711 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2712 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2713 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2714 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2715 | if (address != "") { |
| 2716 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2717 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2718 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2719 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2720 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2721 | } else if (status != NO_ERROR) { |
| 2722 | // Restore client activity state. |
| 2723 | inputDesc->setClientActive(client, false); |
| 2724 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2727 | ALOGV("%s input %d source = %d status = %d exit", |
| 2728 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2729 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2730 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2733 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2734 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2735 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2736 | |
| 2737 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2738 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2739 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2740 | return BAD_VALUE; |
| 2741 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2742 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2743 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2744 | if (!client->active()) { |
| 2745 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2746 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2747 | } |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2748 | auto old_source = inputDesc->source(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2749 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2750 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2751 | inputDesc->stop(); |
| 2752 | if (inputDesc->isActive()) { |
Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2753 | auto current_source = inputDesc->source(); |
| 2754 | setInputDevice(input, getNewInputDevice(inputDesc), |
| 2755 | old_source != current_source /* force */); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2756 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2757 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2758 | // 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] | 2759 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2760 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2761 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2762 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2763 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2764 | |
| 2765 | // automatically disable the remote submix output when input is stopped if not |
| 2766 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2767 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2768 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2769 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2770 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2771 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2772 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2773 | } |
| 2774 | if (address != "") { |
| 2775 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2776 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2777 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2778 | } |
| 2779 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2780 | resetInputDevice(input); |
| 2781 | |
| 2782 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2783 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2784 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2785 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2786 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2787 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2788 | } |
| 2789 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2790 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2791 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2794 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2795 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2796 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2797 | |
| 2798 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2799 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2800 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2801 | return; |
| 2802 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2803 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2804 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2805 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2806 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2807 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2808 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2809 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2810 | if (inputDesc->getClientCount() > 0) { |
| 2811 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2812 | return; |
| 2813 | } |
| 2814 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2815 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2816 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2817 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2818 | } |
| 2819 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2820 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2821 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2822 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2823 | |
| 2824 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2825 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2826 | } |
| 2827 | } |
| 2828 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2829 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2830 | { |
| 2831 | stopInput(portId); |
| 2832 | releaseInput(portId); |
| 2833 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2834 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2835 | void AudioPolicyManager::checkCloseInputs() { |
| 2836 | // After connecting or disconnecting an input device, close input if: |
| 2837 | // - it has no client (was just opened to check profile) OR |
| 2838 | // - none of its supported devices are connected anymore OR |
| 2839 | // - one of its clients cannot be routed to one of its supported |
| 2840 | // devices anymore. Otherwise update device selection |
| 2841 | std::vector<audio_io_handle_t> inputsToClose; |
| 2842 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2843 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 2844 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 2845 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2846 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 2847 | } else { |
| 2848 | bool close = false; |
| 2849 | for (const auto& client : input->clientsList()) { |
| 2850 | sp<DeviceDescriptor> device = |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 2851 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid()); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2852 | if (!input->supportedDevices().contains(device)) { |
| 2853 | close = true; |
| 2854 | break; |
| 2855 | } |
| 2856 | } |
| 2857 | if (close) { |
| 2858 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 2859 | } else { |
| 2860 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | for (const audio_io_handle_t handle : inputsToClose) { |
| 2866 | ALOGV("%s closing input %d", __func__, handle); |
| 2867 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2868 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 2871 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2872 | { |
| 2873 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 2874 | if (indexMin < 0 || indexMax < 0) { |
| 2875 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 2876 | return; |
| 2877 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2878 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2879 | |
| 2880 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2881 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2882 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2883 | continue; |
| 2884 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2885 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2886 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2889 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2890 | int index, |
| 2891 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2892 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2893 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 2894 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 2895 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 2896 | return NO_ERROR; |
| 2897 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2898 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 2899 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2900 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2901 | } |
| 2902 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2903 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2904 | int *index, |
| 2905 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2906 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2907 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 2908 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2909 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2910 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2911 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 2912 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2913 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2914 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2917 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2918 | int index, |
| 2919 | audio_devices_t device) |
| 2920 | { |
| 2921 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2922 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 2923 | if (group == VOLUME_GROUP_NONE) { |
| 2924 | 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] | 2925 | return BAD_VALUE; |
| 2926 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2927 | 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] | 2928 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2929 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2930 | VolumeSource vs = toVolumeSource(group); |
| 2931 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 2932 | |
| 2933 | status = setVolumeCurveIndex(index, device, curves); |
| 2934 | if (status != NO_ERROR) { |
| 2935 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 2936 | return status; |
| 2937 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2938 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2939 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2940 | auto curCurvAttrs = curves.getAttributes(); |
| 2941 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 2942 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2943 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2944 | } else if (!curves.getStreamTypes().empty()) { |
| 2945 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2946 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2947 | } else { |
| 2948 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 2949 | return BAD_VALUE; |
| 2950 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2951 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 2952 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2953 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2954 | // update volume on all outputs and streams matching the following: |
| 2955 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2956 | // - The device (or devices) selected by the engine for this stream includes |
| 2957 | // the requested device |
| 2958 | // - For non default requested device, currently selected device on the output is either the |
| 2959 | // requested device or one of the devices selected by the engine for this stream |
| 2960 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2961 | // no specific device volume value exists for currently selected device. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2962 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2963 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2964 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2965 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2966 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 2967 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 2968 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2969 | if (!(desc->isActive(vs) || isInCall())) { |
| 2970 | continue; |
| 2971 | } |
| 2972 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 2973 | curDevices.find(device) == curDevices.end()) { |
| 2974 | continue; |
| 2975 | } |
| 2976 | bool applyVolume = false; |
| 2977 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 2978 | curSrcDevices.insert(device); |
| 2979 | applyVolume = (curSrcDevices.find( |
| 2980 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); |
| 2981 | } else { |
| 2982 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 2983 | } |
| 2984 | if (!applyVolume) { |
| 2985 | continue; // next output |
| 2986 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2987 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 2988 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 2989 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2990 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2991 | applyVolume = false; |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 2992 | // If the volume source is active with higher priority source, ensure at least Sw Muted |
| 2993 | desc->setSwMute((index == 0), vs, curves.getStreamTypes(), curDevices, 0 /*delayMs*/); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2994 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 2995 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 2996 | false /*preferredDevice*/); |
| 2997 | if (activeClients.empty()) { |
| 2998 | continue; |
| 2999 | } |
| 3000 | bool isPreempted = false; |
| 3001 | bool isHigherPriority = productStrategy < strategy; |
| 3002 | for (const auto &client : activeClients) { |
| 3003 | if (isHigherPriority && (client->volumeSource() != vs)) { |
| 3004 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 3005 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 3006 | " higher priority source active:\n" |
| 3007 | " volumeGroup=%d attributes=%s) \n" |
| 3008 | " on output %zu, bailing out", __func__, productStrategy, |
| 3009 | group, group, toString(attributes).c_str(), |
| 3010 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 3011 | applyVolume = false; |
| 3012 | isPreempted = true; |
| 3013 | break; |
| 3014 | } |
| 3015 | // However, continue for loop to ensure no higher prio clients running on output |
| 3016 | if (client->volumeSource() == vs) { |
| 3017 | applyVolume = true; |
| 3018 | } |
| 3019 | } |
| 3020 | if (isPreempted || applyVolume) { |
| 3021 | break; |
| 3022 | } |
| 3023 | } |
| 3024 | if (!applyVolume) { |
| 3025 | continue; // next output |
| 3026 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3027 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3028 | //FIXME: workaround for truncated touch sounds |
| 3029 | // delayed volume change for system stream to be removed when the problem is |
| 3030 | // handled by system UI |
| 3031 | status_t volStatus = checkAndSetVolume( |
| 3032 | curves, vs, index, desc, curDevices, |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3033 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM, false))? |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 3034 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 3035 | if (volStatus != NO_ERROR) { |
| 3036 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3037 | } |
| 3038 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3039 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 3040 | return status; |
| 3041 | } |
| 3042 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3043 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3044 | audio_devices_t device, |
| 3045 | IVolumeCurves &volumeCurves) |
| 3046 | { |
| 3047 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 3048 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3049 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 3050 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3051 | (index > volumeCurves.getVolumeIndexMax())) { |
| 3052 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 3053 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 3054 | return BAD_VALUE; |
| 3055 | } |
| 3056 | if (!audio_is_output_device(device)) { |
| 3057 | return BAD_VALUE; |
| 3058 | } |
| 3059 | |
| 3060 | // Force max volume if stream cannot be muted |
| 3061 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 3062 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3063 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3064 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 3065 | return NO_ERROR; |
| 3066 | } |
| 3067 | |
| 3068 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3069 | int &index, |
| 3070 | audio_devices_t device) |
| 3071 | { |
| 3072 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 3073 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3074 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3075 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3076 | DeviceTypeSet deviceTypes = mEngine->getOutputDevicesForAttributes( |
| 3077 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3078 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3079 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 3083 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3084 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3085 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3086 | if (isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 3087 | return BAD_VALUE; |
| 3088 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 3089 | index = curves.getVolumeIndex(deviceTypes); |
| 3090 | 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] | 3091 | return NO_ERROR; |
| 3092 | } |
| 3093 | |
| 3094 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3095 | int &index) |
| 3096 | { |
| 3097 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 3098 | return NO_ERROR; |
| 3099 | } |
| 3100 | |
| 3101 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 3102 | int &index) |
| 3103 | { |
| 3104 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 3105 | return NO_ERROR; |
| 3106 | } |
| 3107 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3108 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3109 | { |
| 3110 | // select one output among several suitable for global effects. |
| 3111 | // The priority is as follows: |
| 3112 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 3113 | // AudioFlinger will invalidate the track and the offloaded output |
| 3114 | // will be closed causing the effect to be moved to a PCM output. |
| 3115 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3116 | // 3: The primary output |
| 3117 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3118 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3119 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 3120 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3121 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3122 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3123 | if (outputs.size() == 0) { |
| 3124 | return AUDIO_IO_HANDLE_NONE; |
| 3125 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3126 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3127 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3128 | bool activeOnly = true; |
| 3129 | |
| 3130 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 3131 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 3132 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 3133 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 3134 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3135 | for (audio_io_handle_t output : outputs) { |
| 3136 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 3137 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3138 | continue; |
| 3139 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3140 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 3141 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3142 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3143 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3144 | } |
| 3145 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3146 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3147 | } |
| 3148 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3149 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3150 | } |
| 3151 | } |
| 3152 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 3153 | output = outputOffloaded; |
| 3154 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 3155 | output = outputDeepBuffer; |
| 3156 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 3157 | output = outputPrimary; |
| 3158 | } else { |
| 3159 | output = outputs[0]; |
| 3160 | } |
| 3161 | activeOnly = false; |
| 3162 | } |
| 3163 | |
| 3164 | if (output != mMusicEffectOutput) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3165 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3166 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 3167 | mMusicEffectOutput = output; |
| 3168 | } |
| 3169 | |
| 3170 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3171 | return output; |
| 3172 | } |
| 3173 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3174 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 3175 | { |
| 3176 | return selectOutputForMusicEffects(); |
| 3177 | } |
| 3178 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3179 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3180 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 3181 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3182 | int session, |
| 3183 | int id) |
| 3184 | { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3185 | if (session != AUDIO_SESSION_DEVICE) { |
| 3186 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3187 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 3188 | index = mInputs.indexOfKey(io); |
| 3189 | if (index < 0) { |
| 3190 | ALOGW("registerEffect() unknown io %d", io); |
| 3191 | return INVALID_OPERATION; |
| 3192 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3193 | } |
| 3194 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3195 | return mEffects.registerEffect(desc, io, session, id, |
| 3196 | (strategy == streamToStrategy(AUDIO_STREAM_MUSIC) || |
| 3197 | strategy == PRODUCT_STRATEGY_NONE)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3198 | } |
| 3199 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3200 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 3201 | { |
| 3202 | if (mEffects.getEffect(id) == nullptr) { |
| 3203 | return INVALID_OPERATION; |
| 3204 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 3205 | if (mEffects.isEffectEnabled(id)) { |
| 3206 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 3207 | setEffectEnabled(id, false); |
| 3208 | } |
| 3209 | return mEffects.unregisterEffect(id); |
| 3210 | } |
| 3211 | |
| 3212 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 3213 | { |
| 3214 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 3215 | if (effect == nullptr) { |
| 3216 | return INVALID_OPERATION; |
| 3217 | } |
| 3218 | |
| 3219 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 3220 | if (status == NO_ERROR) { |
| 3221 | mInputs.trackEffectEnabled(effect, enabled); |
| 3222 | } |
| 3223 | return status; |
| 3224 | } |
| 3225 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3226 | |
| 3227 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 3228 | { |
| 3229 | mEffects.moveEffects(ids, io); |
| 3230 | return NO_ERROR; |
| 3231 | } |
| 3232 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3233 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3234 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3235 | auto vs = toVolumeSource(stream, false); |
| 3236 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActive(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 3240 | { |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 3241 | auto vs = toVolumeSource(stream, false); |
| 3242 | return vs != VOLUME_SOURCE_NONE ? mOutputs.isActiveRemotely(vs, inPastMs) : false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3245 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3246 | { |
| 3247 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3248 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3249 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3250 | return true; |
| 3251 | } |
| 3252 | } |
| 3253 | return false; |
| 3254 | } |
| 3255 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3256 | // Register a list of custom mixes with their attributes and format. |
| 3257 | // When a mix is registered, corresponding input and output profiles are |
| 3258 | // added to the remote submix hw module. The profile contains only the |
| 3259 | // parameters (sampling rate, format...) specified by the mix. |
| 3260 | // The corresponding input remote submix device is also connected. |
| 3261 | // |
| 3262 | // When a remote submix device is connected, the address is checked to select the |
| 3263 | // appropriate profile and the corresponding input or output stream is opened. |
| 3264 | // |
| 3265 | // When capture starts, getInputForAttr() will: |
| 3266 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3267 | // - 2 if none found, getDeviceForInputSource() will: |
| 3268 | // - 2.1 look for a mix matching the attributes source |
| 3269 | // - 2.2 if none found, default to device selection by policy rules |
| 3270 | // At this time, the corresponding output remote submix device is also connected |
| 3271 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3272 | // after AudioTracks are invalidated |
| 3273 | // |
| 3274 | // When playback starts, getOutputForAttr() will: |
| 3275 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3276 | // - 2 if none found, look for a mix matching the attributes usage |
| 3277 | // - 3 if none found, default to device and output selection by policy rules. |
| 3278 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3279 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3280 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3281 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3282 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3283 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3284 | sp<HwModule> rSubmixModule; |
| 3285 | // examine each mix's route type |
| 3286 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3287 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3288 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3289 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3290 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3291 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3292 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3293 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3294 | break; |
| 3295 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3296 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3297 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3298 | 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] | 3299 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3300 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3301 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3302 | rSubmixModule = mHwModules.getModuleFromName( |
| 3303 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3304 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3305 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3306 | i); |
| 3307 | res = INVALID_OPERATION; |
| 3308 | break; |
| 3309 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3310 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3311 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3312 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3313 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3314 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3315 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3316 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3317 | } else { |
| 3318 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3319 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3320 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3321 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3322 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3323 | ALOGE("Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3324 | res = INVALID_OPERATION; |
| 3325 | break; |
| 3326 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3327 | audio_config_t outputConfig = mix.mFormat; |
| 3328 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3329 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3330 | // 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] | 3331 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3332 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3333 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3334 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3335 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3336 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3337 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3338 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3339 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3340 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
| 3341 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
| 3342 | mix.mDeviceType, address.string()); |
| 3343 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3344 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3345 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3346 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3347 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3348 | 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] | 3349 | i, mixes.size(), type, address.string()); |
| 3350 | |
| 3351 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3352 | mix.mDeviceType, mix.mDeviceAddress, |
| 3353 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3354 | if (device == nullptr) { |
| 3355 | res = INVALID_OPERATION; |
| 3356 | break; |
| 3357 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3358 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3359 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3360 | // First try to find an already opened output supporting the device |
| 3361 | 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] | 3362 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3363 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3364 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3365 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3366 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3367 | address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3368 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3369 | } else { |
| 3370 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3371 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3372 | } |
| 3373 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3374 | // If no output found, try to find a direct output profile supporting the device |
| 3375 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3376 | sp<HwModule> module = mHwModules[i]; |
| 3377 | for (size_t j = 0; |
| 3378 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3379 | j++) { |
| 3380 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3381 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3382 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3383 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3384 | address.string()); |
| 3385 | res = INVALID_OPERATION; |
| 3386 | } else { |
| 3387 | foundOutput = true; |
| 3388 | } |
| 3389 | } |
| 3390 | } |
| 3391 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3392 | if (res != NO_ERROR) { |
| 3393 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3394 | i, type, address.string()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3395 | res = INVALID_OPERATION; |
| 3396 | break; |
| 3397 | } else if (!foundOutput) { |
| 3398 | 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] | 3399 | i, type, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3400 | res = INVALID_OPERATION; |
| 3401 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3402 | } else { |
| 3403 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3404 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3405 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3406 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3407 | if (res != NO_ERROR) { |
| 3408 | unregisterPolicyMixes(mixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3409 | } else if (checkOutputs) { |
| 3410 | checkForDeviceAndOutputChanges(); |
| 3411 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3412 | } |
| 3413 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3417 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3418 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3419 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3420 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3421 | sp<HwModule> rSubmixModule; |
| 3422 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3423 | for (const auto& mix : mixes) { |
| 3424 | 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] | 3425 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3426 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3427 | rSubmixModule = mHwModules.getModuleFromName( |
| 3428 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3429 | if (rSubmixModule == 0) { |
| 3430 | res = INVALID_OPERATION; |
| 3431 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3432 | } |
| 3433 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3434 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3435 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3436 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3437 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3438 | res = INVALID_OPERATION; |
| 3439 | continue; |
| 3440 | } |
| 3441 | |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3442 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
| 3443 | if (getDeviceConnectionState(device, address.string()) == |
| 3444 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3445 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3446 | address.string(), "remote-submix", |
| 3447 | AUDIO_FORMAT_DEFAULT); |
| 3448 | if (res != OK) { |
| 3449 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
| 3450 | "with type %d, address %s", device, address.string()); |
| 3451 | } |
| 3452 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3453 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3454 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3455 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3456 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3457 | } 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] | 3458 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3459 | res = INVALID_OPERATION; |
| 3460 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3461 | } else { |
| 3462 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3463 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3464 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3465 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3466 | if (res == NO_ERROR && checkOutputs) { |
| 3467 | checkForDeviceAndOutputChanges(); |
| 3468 | updateCallAndOutputRouting(); |
| 3469 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3470 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3471 | } |
| 3472 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3473 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3474 | { |
| 3475 | size_t i = 0; |
| 3476 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3477 | for (const auto& fmt : mManualSurroundFormats) { |
| 3478 | if (i++ != 0) dst->append(", "); |
| 3479 | std::string sfmt; |
| 3480 | FormatConverter::toString(fmt, sfmt); |
| 3481 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3482 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3483 | } |
| 3484 | } |
| 3485 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3486 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3487 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3488 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3489 | std::function<bool(audio_devices_t)> predicate, |
| 3490 | const char *context) { |
| 3491 | for (size_t i = 0; i < devices.size(); i++) { |
| 3492 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3493 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 0e26e3f | 2020-04-29 14:24:16 -0700 | [diff] [blame] | 3494 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, true /*matchAddress*/); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3495 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3496 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3497 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3498 | return false; |
| 3499 | } |
| 3500 | } |
| 3501 | return true; |
| 3502 | } |
| 3503 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3504 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3505 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3506 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3507 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3508 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3509 | } |
| 3510 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3511 | if (res != NO_ERROR) { |
| 3512 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3513 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3514 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3515 | |
| 3516 | checkForDeviceAndOutputChanges(); |
| 3517 | updateCallAndOutputRouting(); |
| 3518 | |
| 3519 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 3523 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3524 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 3525 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3526 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3527 | __FUNCTION__, uid); |
| 3528 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3529 | } |
| 3530 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3531 | checkForDeviceAndOutputChanges(); |
| 3532 | updateCallAndOutputRouting(); |
| 3533 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3534 | return res; |
| 3535 | } |
| 3536 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3537 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3538 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 3539 | device_role_t role, |
| 3540 | const AudioDeviceTypeAddrVector &devices) { |
| 3541 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3542 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3543 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3544 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3545 | return BAD_VALUE; |
| 3546 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3547 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3548 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3549 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 3550 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3551 | return status; |
| 3552 | } |
| 3553 | |
| 3554 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3555 | |
| 3556 | bool forceVolumeReeval = false; |
| 3557 | // FIXME: workaround for truncated touch sounds |
| 3558 | // to be removed when the problem is handled by system UI |
| 3559 | uint32_t delayMs = 0; |
| 3560 | if (strategy == mCommunnicationStrategy) { |
| 3561 | forceVolumeReeval = true; |
| 3562 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3563 | updateInputRouting(); |
| 3564 | } |
| 3565 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3566 | |
| 3567 | return NO_ERROR; |
| 3568 | } |
| 3569 | |
| 3570 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs) |
| 3571 | { |
| 3572 | uint32_t waitMs = 0; |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 3573 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3574 | // Only apply special touch sound delay once |
| 3575 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3576 | } |
| 3577 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3578 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3579 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3580 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 3581 | (outputDesc != mPrimaryOutput && !isTelephonyRxOrTx(outputDesc))) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3582 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 3583 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 3584 | // Let's give back to engine full device choice decision however. |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 3585 | bool forceRouting = !newDevices.isEmpty(); |
| 3586 | waitMs = setOutputDevices(outputDesc, newDevices, forceRouting, delayMs, nullptr, |
| 3587 | true /*requiresMuteCheck*/, |
| 3588 | !forceRouting /*requiresVolumeCheck*/); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3589 | // Only apply special touch sound delay once |
| 3590 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3591 | } |
| 3592 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 3593 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 3594 | } |
| 3595 | } |
| 3596 | } |
| 3597 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3598 | void AudioPolicyManager::updateInputRouting() { |
| 3599 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3600 | // Skip for hotword recording as the input device switch |
| 3601 | // is handled within sound trigger HAL |
| 3602 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 3603 | continue; |
| 3604 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3605 | auto newDevice = getNewInputDevice(activeDesc); |
| 3606 | // Force new input selection if the new device can not be reached via current input |
| 3607 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 3608 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 3609 | } else { |
| 3610 | closeInput(activeDesc->mIoHandle); |
| 3611 | } |
| 3612 | } |
| 3613 | } |
| 3614 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3615 | status_t AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 3616 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3617 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3618 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3619 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3620 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3621 | if (status != NO_ERROR) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3622 | ALOGV("Engine could not remove preferred device for strategy %d status %d", |
| 3623 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3624 | return status; |
| 3625 | } |
| 3626 | |
| 3627 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3628 | |
| 3629 | bool forceVolumeReeval = false; |
| 3630 | // FIXME: workaround for truncated touch sounds |
| 3631 | // to be removed when the problem is handled by system UI |
| 3632 | uint32_t delayMs = 0; |
| 3633 | if (strategy == mCommunnicationStrategy) { |
| 3634 | forceVolumeReeval = true; |
| 3635 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3636 | updateInputRouting(); |
| 3637 | } |
| 3638 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3639 | |
| 3640 | return NO_ERROR; |
| 3641 | } |
| 3642 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3643 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 3644 | device_role_t role, |
| 3645 | AudioDeviceTypeAddrVector &devices) { |
| 3646 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3647 | } |
| 3648 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3649 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 3650 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3651 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3652 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3653 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3654 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3655 | return BAD_VALUE; |
| 3656 | } |
| 3657 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3658 | ALOGW_IF(status != NO_ERROR, |
| 3659 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 3660 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3661 | |
| 3662 | return status; |
| 3663 | } |
| 3664 | |
| 3665 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 3666 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3667 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3668 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3669 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3670 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3671 | return BAD_VALUE; |
| 3672 | } |
| 3673 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3674 | ALOGW_IF(status != NO_ERROR, |
| 3675 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 3676 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3677 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3678 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3679 | return status; |
| 3680 | } |
| 3681 | |
| 3682 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 3683 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 3684 | { |
| 3685 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 3686 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3687 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3688 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3689 | return BAD_VALUE; |
| 3690 | } |
| 3691 | |
| 3692 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 3693 | audioSource, role, devices); |
| 3694 | ALOGW_IF(status != NO_ERROR, |
| 3695 | "Engine could not remove devices role (%d) for capture preset %d", role, audioSource); |
| 3696 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3697 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3698 | return status; |
| 3699 | } |
| 3700 | |
| 3701 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 3702 | device_role_t role) { |
| 3703 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 3704 | |
| 3705 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
| 3706 | ALOGW_IF(status != NO_ERROR, |
| 3707 | "Engine could not clear devices role (%d) for capture preset %d", role, audioSource); |
| 3708 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3709 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3710 | return status; |
| 3711 | } |
| 3712 | |
| 3713 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 3714 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 3715 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 3716 | } |
| 3717 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3718 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3719 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3720 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3721 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3722 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3723 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3724 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 3725 | if (status != NO_ERROR) { |
| 3726 | ALOGE("%s() could not set device affinity for userId %d", |
| 3727 | __FUNCTION__, userId); |
| 3728 | return status; |
| 3729 | } |
| 3730 | |
| 3731 | // reevaluate outputs for all devices |
| 3732 | checkForDeviceAndOutputChanges(); |
| 3733 | updateCallAndOutputRouting(); |
| 3734 | |
| 3735 | return NO_ERROR; |
| 3736 | } |
| 3737 | |
| 3738 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3739 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3740 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 3741 | if (status != NO_ERROR) { |
| 3742 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 3743 | __FUNCTION__, userId); |
| 3744 | return status; |
| 3745 | } |
| 3746 | |
| 3747 | // reevaluate outputs for all devices |
| 3748 | checkForDeviceAndOutputChanges(); |
| 3749 | updateCallAndOutputRouting(); |
| 3750 | |
| 3751 | return NO_ERROR; |
| 3752 | } |
| 3753 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3754 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3755 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3756 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
Mikhail Naganov | 0dbe87b | 2021-12-01 02:03:31 +0000 | [diff] [blame] | 3757 | dst->appendFormat(" Primary Output I/O handle: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3758 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 3759 | std::string stateLiteral; |
| 3760 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3761 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3762 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 3763 | "communications", "media", "record", "dock", "system", |
| 3764 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 3765 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 3766 | 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] | 3767 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 3768 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 3769 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 3770 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3771 | dst->append(" (MANUAL: "); |
| 3772 | dumpManualSurroundFormats(dst); |
| 3773 | dst->append(")"); |
| 3774 | } |
| 3775 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3776 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3777 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 3778 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Mikhail Naganov | b3bcb4f | 2022-02-23 23:46:56 +0000 | [diff] [blame] | 3779 | dst->appendFormat(" Communication Strategy id: %d\n", mCommunnicationStrategy); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3780 | 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] | 3781 | |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 3782 | dst->append("\n"); |
| 3783 | mAvailableOutputDevices.dump(dst, String8("Available output"), 1); |
| 3784 | dst->append("\n"); |
| 3785 | mAvailableInputDevices.dump(dst, String8("Available input"), 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3786 | mHwModulesAll.dump(dst); |
| 3787 | mOutputs.dump(dst); |
| 3788 | mInputs.dump(dst); |
Mikhail Naganov | 0f413b2 | 2021-12-02 05:29:27 +0000 | [diff] [blame] | 3789 | mEffects.dump(dst, 1); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3790 | mAudioPatches.dump(dst); |
| 3791 | mPolicyMixes.dump(dst); |
| 3792 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3793 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3794 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 3795 | for (auto& policy : mAllowedCapturePolicies) { |
| 3796 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 3797 | } |
| 3798 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3799 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 3800 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | status_t AudioPolicyManager::dump(int fd) |
| 3804 | { |
| 3805 | String8 result; |
| 3806 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 3807 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3808 | return NO_ERROR; |
| 3809 | } |
| 3810 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3811 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 3812 | { |
| 3813 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 3814 | return NO_ERROR; |
| 3815 | } |
| 3816 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3817 | // This function checks for the parameters which can be offloaded. |
| 3818 | // This can be enhanced depending on the capability of the DSP and policy |
| 3819 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3820 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3821 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3822 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3823 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3824 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3825 | offloadInfo.format, |
| 3826 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 3827 | offloadInfo.has_video); |
| 3828 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3829 | if (!isOffloadPossible(offloadInfo)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3830 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | // See if there is a profile to support this. |
| 3834 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3835 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3836 | offloadInfo.sample_rate, |
| 3837 | offloadInfo.format, |
| 3838 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3839 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 3840 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 3841 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 3842 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 3843 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3844 | if (profile == nullptr) { |
| 3845 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 3846 | } |
| 3847 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 3848 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 3849 | } |
| 3850 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3851 | } |
| 3852 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3853 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 3854 | const audio_attributes_t& attributes) { |
| 3855 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 3856 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
Dorin Drimus | 9901eb0 | 2022-01-14 11:36:51 +0000 | [diff] [blame] | 3857 | DeviceVector outputDevices = mEngine->getOutputDevicesForAttributes(attributes); |
| 3858 | sp<IOProfile> profile = getProfileForOutput(outputDevices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3859 | config.sample_rate, |
| 3860 | config.format, |
| 3861 | config.channel_mask, |
| 3862 | output_flags, |
| 3863 | true /* directOnly */); |
| 3864 | ALOGV("%s() profile %sfound with name: %s, " |
| 3865 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 3866 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3867 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3868 | config.sample_rate, config.format, config.channel_mask, output_flags); |
Dorin Drimus | ecc9f42 | 2022-03-09 17:57:40 +0100 | [diff] [blame] | 3869 | |
| 3870 | // also try the MSD module if compatible profile not found |
| 3871 | if (profile == nullptr) { |
| 3872 | profile = getMsdProfileForOutput(outputDevices, |
| 3873 | config.sample_rate, |
| 3874 | config.format, |
| 3875 | config.channel_mask, |
| 3876 | output_flags, |
| 3877 | true /* directOnly */); |
| 3878 | ALOGV("%s() MSD profile %sfound with name: %s, " |
| 3879 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 3880 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 3881 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
| 3882 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 3883 | } |
| 3884 | return (profile != nullptr); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3885 | } |
| 3886 | |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3887 | bool AudioPolicyManager::isOffloadPossible(const audio_offload_info_t &offloadInfo, |
| 3888 | bool durationIgnored) { |
| 3889 | if (mMasterMono) { |
| 3890 | return false; // no offloading if mono is set. |
| 3891 | } |
| 3892 | |
| 3893 | // Check if offload has been disabled |
| 3894 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 3895 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 3896 | return false; |
| 3897 | } |
| 3898 | |
| 3899 | // Check if stream type is music, then only allow offload as of now. |
| 3900 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 3901 | { |
| 3902 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 3903 | return false; |
| 3904 | } |
| 3905 | |
| 3906 | //TODO: enable audio offloading with video when ready |
| 3907 | const bool allowOffloadWithVideo = |
| 3908 | property_get_bool("audio.offload.video", false /* default_value */); |
| 3909 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
| 3910 | ALOGV("%s: has_video == true, returning false", __func__); |
| 3911 | return false; |
| 3912 | } |
| 3913 | |
| 3914 | //If duration is less than minimum value defined in property, return false |
| 3915 | const int min_duration_secs = property_get_int32( |
| 3916 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 3917 | if (!durationIgnored) { |
| 3918 | if (min_duration_secs >= 0) { |
| 3919 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 3920 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 3921 | __func__, min_duration_secs); |
| 3922 | return false; |
| 3923 | } |
| 3924 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 3925 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 3926 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 3927 | return false; |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 3932 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 3933 | // detects there is an active non offloadable effect. |
| 3934 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 3935 | // This may prevent offloading in rare situations where effects are left active by apps |
| 3936 | // in the background. |
| 3937 | if (mEffects.isNonOffloadableEffectEnabled()) { |
| 3938 | return false; |
| 3939 | } |
| 3940 | |
| 3941 | return true; |
| 3942 | } |
| 3943 | |
| 3944 | audio_direct_mode_t AudioPolicyManager::getDirectPlaybackSupport(const audio_attributes_t *attr, |
| 3945 | const audio_config_t *config) { |
| 3946 | audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; |
| 3947 | offloadInfo.format = config->format; |
| 3948 | offloadInfo.sample_rate = config->sample_rate; |
| 3949 | offloadInfo.channel_mask = config->channel_mask; |
| 3950 | offloadInfo.stream_type = mEngine->getStreamTypeForAttributes(*attr); |
| 3951 | offloadInfo.has_video = false; |
| 3952 | offloadInfo.is_streaming = false; |
| 3953 | const bool offloadPossible = isOffloadPossible(offloadInfo, true /*durationIgnored*/); |
| 3954 | |
| 3955 | audio_direct_mode_t directMode = AUDIO_DIRECT_NOT_SUPPORTED; |
| 3956 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 3957 | audio_flags_to_audio_output_flags(attr->flags, &flags); |
| 3958 | // only retain flags that will drive compressed offload or passthrough |
| 3959 | uint32_t relevantFlags = AUDIO_OUTPUT_FLAG_HW_AV_SYNC; |
| 3960 | if (offloadPossible) { |
| 3961 | relevantFlags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; |
| 3962 | } |
| 3963 | flags = (audio_output_flags_t)((flags & relevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 3964 | |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame^] | 3965 | DeviceVector engineOutputDevices = mEngine->getOutputDevicesForAttributes(*attr); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3966 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame^] | 3967 | DeviceVector outputDevices = engineOutputDevices; |
| 3968 | // the MSD module checks for different conditions and output devices |
| 3969 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 3970 | if (!msdHasPatchesToAllDevices(engineOutputDevices.toTypeAddrVector())) { |
| 3971 | continue; |
| 3972 | } |
| 3973 | outputDevices = getMsdAudioOutDevices(); |
| 3974 | } |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3975 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
jiabin | c8f7dfc | 2022-01-06 18:42:08 +0000 | [diff] [blame] | 3976 | if (!curProfile->isCompatibleProfile(outputDevices, |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3977 | config->sample_rate, nullptr /*updatedSamplingRate*/, |
| 3978 | config->format, nullptr /*updatedFormat*/, |
| 3979 | config->channel_mask, nullptr /*updatedChannelMask*/, |
| 3980 | flags)) { |
| 3981 | continue; |
| 3982 | } |
| 3983 | // reject profiles not corresponding to a device currently available |
| 3984 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
| 3985 | continue; |
| 3986 | } |
| 3987 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) |
| 3988 | != AUDIO_OUTPUT_FLAG_NONE) { |
jiabin | c6132d6 | 2022-01-01 07:36:31 +0000 | [diff] [blame] | 3989 | if ((directMode & AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED) |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 3990 | != AUDIO_DIRECT_NOT_SUPPORTED) { |
| 3991 | // Already reports offload gapless supported. No need to report offload support. |
| 3992 | continue; |
| 3993 | } |
| 3994 | if ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) |
| 3995 | != AUDIO_OUTPUT_FLAG_NONE) { |
| 3996 | // If offload gapless is reported, no need to report offload support. |
| 3997 | directMode = (audio_direct_mode_t) ((directMode & |
| 3998 | ~AUDIO_DIRECT_OFFLOAD_SUPPORTED) | |
| 3999 | AUDIO_DIRECT_OFFLOAD_GAPLESS_SUPPORTED); |
| 4000 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame^] | 4001 | directMode = (audio_direct_mode_t)(directMode | AUDIO_DIRECT_OFFLOAD_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4002 | } |
| 4003 | } else { |
Dorin Drimus | fae3c64 | 2022-03-17 18:36:30 +0100 | [diff] [blame^] | 4004 | directMode = (audio_direct_mode_t) (directMode | AUDIO_DIRECT_BITSTREAM_SUPPORTED); |
jiabin | 2b9d5a1 | 2021-12-10 01:06:29 +0000 | [diff] [blame] | 4005 | } |
| 4006 | } |
| 4007 | } |
| 4008 | return directMode; |
| 4009 | } |
| 4010 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4011 | status_t AudioPolicyManager::getDirectProfilesForAttributes(const audio_attributes_t* attr, |
| 4012 | AudioProfileVector& audioProfilesVector) { |
| 4013 | AudioDeviceTypeAddrVector devices; |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 4014 | status_t status = getDevicesForAttributes(*attr, &devices, false /* forVolume */); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4015 | if (status != OK) { |
| 4016 | return status; |
| 4017 | } |
| 4018 | ALOGV("%s: found %zu output devices for attributes.", __func__, devices.size()); |
| 4019 | if (devices.empty()) { |
| 4020 | return OK; // no output devices for the attributes |
| 4021 | } |
| 4022 | |
| 4023 | for (const auto& hwModule : mHwModules) { |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 4024 | // the MSD module checks for different conditions |
| 4025 | if (strcmp(hwModule->getName(), AUDIO_HARDWARE_MODULE_ID_MSD) == 0) { |
| 4026 | continue; |
| 4027 | } |
| 4028 | for (const auto& outputProfile : hwModule->getOutputProfiles()) { |
| 4029 | if (!outputProfile->asAudioPort()->isDirectOutput()) { |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4030 | continue; |
| 4031 | } |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 4032 | // allow only profiles that support all the available and routed devices |
| 4033 | if (outputProfile->getSupportedDevices().getDevicesFromDeviceTypeAddrVec(devices).size() |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4034 | != devices.size()) { |
| 4035 | continue; |
| 4036 | } |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 4037 | audioProfilesVector.addAllValidProfiles( |
| 4038 | outputProfile->asAudioPort()->getAudioProfiles()); |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4039 | } |
| 4040 | } |
Dorin Drimus | 94d9441 | 2022-02-02 09:05:02 +0100 | [diff] [blame] | 4041 | |
| 4042 | // add the direct profiles from MSD if present and has audio patches to all the output(s) |
| 4043 | const auto& msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 4044 | if (msdModule != nullptr) { |
| 4045 | if (msdHasPatchesToAllDevices(devices)) { |
| 4046 | ALOGV("%s: MSD audio patches set to all output devices.", __func__); |
| 4047 | for (const auto& outputProfile : msdModule->getOutputProfiles()) { |
| 4048 | if (!outputProfile->asAudioPort()->isDirectOutput()) { |
| 4049 | continue; |
| 4050 | } |
| 4051 | audioProfilesVector.addAllValidProfiles( |
| 4052 | outputProfile->asAudioPort()->getAudioProfiles()); |
| 4053 | } |
| 4054 | } else { |
| 4055 | ALOGV("%s: MSD audio patches NOT set to all output devices.", __func__); |
| 4056 | } |
| 4057 | } |
| 4058 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 4059 | return NO_ERROR; |
| 4060 | } |
| 4061 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4062 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 4063 | audio_port_type_t type, |
| 4064 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4065 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4066 | unsigned int *generation) |
| 4067 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4068 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 4069 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4070 | return BAD_VALUE; |
| 4071 | } |
| 4072 | 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] | 4073 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4074 | *num_ports = 0; |
| 4075 | } |
| 4076 | |
| 4077 | size_t portsWritten = 0; |
| 4078 | size_t portsMax = *num_ports; |
| 4079 | *num_ports = 0; |
| 4080 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4081 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 4082 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4083 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4084 | for (const auto& dev : mAvailableOutputDevices) { |
| 4085 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4086 | continue; |
| 4087 | } |
| 4088 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4089 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4090 | } |
| 4091 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4092 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4093 | } |
| 4094 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4095 | for (const auto& dev : mAvailableInputDevices) { |
| 4096 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4097 | continue; |
| 4098 | } |
| 4099 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4100 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 4101 | } |
| 4102 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4103 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4104 | } |
| 4105 | } |
| 4106 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 4107 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 4108 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 4109 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4110 | } |
| 4111 | *num_ports += mInputs.size(); |
| 4112 | } |
| 4113 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4114 | size_t numOutputs = 0; |
| 4115 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4116 | if (!mOutputs[i]->isDuplicated()) { |
| 4117 | numOutputs++; |
| 4118 | if (portsWritten < portsMax) { |
| 4119 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 4120 | } |
| 4121 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4122 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4123 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4127 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4128 | return NO_ERROR; |
| 4129 | } |
| 4130 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 4131 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4132 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 4133 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 4134 | return BAD_VALUE; |
| 4135 | } |
| 4136 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 4137 | if (dev != 0) { |
| 4138 | dev->toAudioPort(port); |
| 4139 | return NO_ERROR; |
| 4140 | } |
| 4141 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 4142 | if (dev != 0) { |
| 4143 | dev->toAudioPort(port); |
| 4144 | return NO_ERROR; |
| 4145 | } |
| 4146 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 4147 | if (out != 0) { |
| 4148 | out->toAudioPort(port); |
| 4149 | return NO_ERROR; |
| 4150 | } |
| 4151 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 4152 | if (in != 0) { |
| 4153 | in->toAudioPort(port); |
| 4154 | return NO_ERROR; |
| 4155 | } |
| 4156 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4157 | } |
| 4158 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4159 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 4160 | audio_patch_handle_t *handle, |
| 4161 | uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4162 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4163 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4164 | if (handle == NULL || patch == NULL) { |
| 4165 | return BAD_VALUE; |
| 4166 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4167 | 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] | 4168 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4169 | return BAD_VALUE; |
| 4170 | } |
| 4171 | // only one source per audio patch supported for now |
| 4172 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4173 | return INVALID_OPERATION; |
| 4174 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4175 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4176 | return INVALID_OPERATION; |
| 4177 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4178 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4179 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 4180 | return INVALID_OPERATION; |
| 4181 | } |
| 4182 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4183 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4184 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 4185 | sp<DeviceDescriptor> sinkDevice = mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id); |
| 4186 | if (srcDevice == nullptr || sinkDevice == nullptr) { |
| 4187 | ALOGW("%s could not create patch, invalid sink and/or source device(s)", __func__); |
| 4188 | return BAD_VALUE; |
| 4189 | } |
| 4190 | ALOGV("%s between source %s and sink %s", __func__, |
| 4191 | srcDevice->toString().c_str(), sinkDevice->toString().c_str()); |
| 4192 | audio_port_handle_t portId = PolicyAudioPort::getNextUniqueId(); |
| 4193 | // Default attributes, default volume priority, not to infer with non raw audio patches. |
| 4194 | audio_attributes_t attributes = attributes_initializer(AUDIO_USAGE_MEDIA); |
| 4195 | const struct audio_port_config *source = &patch->sources[0]; |
| 4196 | sp<SourceClientDescriptor> sourceDesc = |
| 4197 | new InternalSourceClientDescriptor( |
| 4198 | portId, uid, attributes, *source, srcDevice, sinkDevice, |
| 4199 | mEngine->getProductStrategyForAttributes(attributes), toVolumeSource(attributes)); |
| 4200 | |
| 4201 | status_t status = |
| 4202 | connectAudioSourceToSink(sourceDesc, sinkDevice, patch, *handle, uid, 0 /* delayMs */); |
| 4203 | |
| 4204 | if (status != NO_ERROR) { |
| 4205 | return INVALID_OPERATION; |
| 4206 | } |
| 4207 | mAudioSources.add(portId, sourceDesc); |
| 4208 | return NO_ERROR; |
| 4209 | } |
| 4210 | |
| 4211 | status_t AudioPolicyManager::connectAudioSourceToSink( |
| 4212 | const sp<SourceClientDescriptor>& sourceDesc, const sp<DeviceDescriptor> &sinkDevice, |
| 4213 | const struct audio_patch *patch, |
| 4214 | audio_patch_handle_t &handle, |
| 4215 | uid_t uid, uint32_t delayMs) |
| 4216 | { |
| 4217 | status_t status = createAudioPatchInternal(patch, &handle, uid, delayMs, sourceDesc); |
| 4218 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4219 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4220 | return INVALID_OPERATION; |
| 4221 | } |
| 4222 | sourceDesc->connect(handle, sinkDevice); |
| 4223 | if (isMsdPatch(handle)) { |
| 4224 | return NO_ERROR; |
| 4225 | } |
| 4226 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4227 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4228 | ALOG_ASSERT(swOutput != nullptr, "%s: a swOutput shall always be associated", __func__); |
| 4229 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
| 4230 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 4231 | goto FailurePatchAdded; |
| 4232 | } |
| 4233 | status = swOutput->start(); |
| 4234 | if (status != NO_ERROR) { |
| 4235 | goto FailureSourceAdded; |
| 4236 | } |
| 4237 | swOutput->addClient(sourceDesc); |
| 4238 | status = startSource(swOutput, sourceDesc, &delayMs); |
| 4239 | if (status != NO_ERROR) { |
| 4240 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4241 | goto FailureSourceActive; |
| 4242 | } |
| 4243 | if (delayMs != 0) { |
| 4244 | usleep(delayMs * 1000); |
| 4245 | } |
| 4246 | return NO_ERROR; |
| 4247 | |
| 4248 | FailureSourceActive: |
| 4249 | swOutput->stop(); |
| 4250 | releaseOutput(sourceDesc->portId()); |
| 4251 | FailureSourceAdded: |
| 4252 | sourceDesc->setSwOutput(nullptr); |
| 4253 | FailurePatchAdded: |
| 4254 | releaseAudioPatchInternal(handle); |
| 4255 | return INVALID_OPERATION; |
| 4256 | } |
| 4257 | |
| 4258 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 4259 | audio_patch_handle_t *handle, |
| 4260 | uid_t uid, uint32_t delayMs, |
| 4261 | const sp<SourceClientDescriptor>& sourceDesc) |
| 4262 | { |
| 4263 | 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] | 4264 | sp<AudioPatch> patchDesc; |
| 4265 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 4266 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4267 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 4268 | patch->sources[0].role, |
| 4269 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4270 | #if LOG_NDEBUG == 0 |
| 4271 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4272 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 4273 | patch->sinks[i].role, |
| 4274 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4275 | } |
| 4276 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4277 | |
| 4278 | if (index >= 0) { |
| 4279 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4280 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 4281 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4282 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4283 | return INVALID_OPERATION; |
| 4284 | } |
| 4285 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4286 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4290 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4291 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4292 | 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] | 4293 | return BAD_VALUE; |
| 4294 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4295 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 4296 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4297 | if (patchDesc != 0) { |
| 4298 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4299 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 4300 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4301 | return BAD_VALUE; |
| 4302 | } |
| 4303 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4304 | DeviceVector devices; |
| 4305 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4306 | // Only support mix to devices connection |
| 4307 | // TODO add support for mix to mix connection |
| 4308 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4309 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4310 | return INVALID_OPERATION; |
| 4311 | } |
| 4312 | sp<DeviceDescriptor> devDesc = |
| 4313 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 4314 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4315 | 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] | 4316 | return BAD_VALUE; |
| 4317 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4318 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4319 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4320 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4321 | NULL, // updatedSamplingRate |
| 4322 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4323 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4324 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4325 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4326 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4327 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4328 | return INVALID_OPERATION; |
| 4329 | } |
| 4330 | devices.add(devDesc); |
| 4331 | } |
| 4332 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4333 | return INVALID_OPERATION; |
| 4334 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4335 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4336 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4337 | ALOGV("%s setting device %s on output %d", |
| 4338 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4339 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4340 | index = mAudioPatches.indexOfKey(*handle); |
| 4341 | if (index >= 0) { |
| 4342 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4343 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4344 | } |
| 4345 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4346 | patchDesc->setUid(uid); |
| 4347 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4348 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4349 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4350 | return INVALID_OPERATION; |
| 4351 | } |
| 4352 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4353 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 4354 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4355 | // only one sink supported when connecting an input device to a mix |
| 4356 | if (patch->num_sinks > 1) { |
| 4357 | return INVALID_OPERATION; |
| 4358 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4359 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4360 | if (inputDesc == NULL) { |
| 4361 | return BAD_VALUE; |
| 4362 | } |
| 4363 | if (patchDesc != 0) { |
| 4364 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 4365 | return BAD_VALUE; |
| 4366 | } |
| 4367 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4368 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4369 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4370 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4371 | return BAD_VALUE; |
| 4372 | } |
| 4373 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4374 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4375 | patch->sinks[0].sample_rate, |
| 4376 | NULL, /*updatedSampleRate*/ |
| 4377 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4378 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4379 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4380 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4381 | // FIXME for the parameter type, |
| 4382 | // and the NONE |
| 4383 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4384 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4385 | return INVALID_OPERATION; |
| 4386 | } |
| 4387 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4388 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4389 | device->toString().c_str(), inputDesc->mIoHandle); |
| 4390 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4391 | index = mAudioPatches.indexOfKey(*handle); |
| 4392 | if (index >= 0) { |
| 4393 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4394 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4395 | } |
| 4396 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4397 | patchDesc->setUid(uid); |
| 4398 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4399 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4400 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4401 | return INVALID_OPERATION; |
| 4402 | } |
| 4403 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4404 | // device to device connection |
| 4405 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4406 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4407 | return BAD_VALUE; |
| 4408 | } |
| 4409 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4410 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4411 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4412 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 4413 | return BAD_VALUE; |
| 4414 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4415 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4416 | //update source and sink with our own data as the data passed in the patch may |
| 4417 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4418 | PatchBuilder patchBuilder; |
| 4419 | audio_port_config sourcePortConfig = {}; |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4420 | |
| 4421 | // if first sink is to MSD, establish single MSD patch |
| 4422 | if (getMsdAudioOutDevices().contains( |
| 4423 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { |
| 4424 | ALOGV("%s patching to MSD", __FUNCTION__); |
| 4425 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); |
| 4426 | goto installPatch; |
| 4427 | } |
| 4428 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4429 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 4430 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4431 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4432 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 4433 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4434 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4435 | return INVALID_OPERATION; |
| 4436 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4437 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4438 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4439 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4440 | return BAD_VALUE; |
| 4441 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4442 | audio_port_config sinkPortConfig = {}; |
| 4443 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 4444 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4445 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4446 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 4447 | // volume management purpose (tracking activity) |
| 4448 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 4449 | // in config XML to reach the sink so that is can be declared as available. |
| 4450 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 4451 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4452 | if (!sourceDesc->isInternal()) { |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4453 | // take care of dynamic routing for SwOutput selection, |
| 4454 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 4455 | audio_stream_type_t stream = sourceDesc->stream(); |
| 4456 | audio_attributes_t resultAttr; |
| 4457 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4458 | config.sample_rate = sourceDesc->config().sample_rate; |
| 4459 | config.channel_mask = sourceDesc->config().channel_mask; |
| 4460 | config.format = sourceDesc->config().format; |
| 4461 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 4462 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 4463 | bool isRequestedDeviceForExclusiveUse = false; |
| 4464 | output_type_t outputType; |
| 4465 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 4466 | &stream, sourceDesc->uid(), &config, &flags, |
| 4467 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
| 4468 | nullptr, &outputType); |
| 4469 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4470 | ALOGV("%s no output for device %s", |
| 4471 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 4472 | return INVALID_OPERATION; |
| 4473 | } |
| 4474 | outputDesc = mOutputs.valueFor(output); |
| 4475 | if (outputDesc->isDuplicated()) { |
| 4476 | ALOGE("%s output is duplicated", __func__); |
| 4477 | return INVALID_OPERATION; |
| 4478 | } |
| 4479 | sourceDesc->setSwOutput(outputDesc); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4480 | } else { |
| 4481 | // Same for "raw patches" aka created from createAudioPatch API |
| 4482 | SortedVector<audio_io_handle_t> outputs = |
| 4483 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 4484 | // if the sink device is reachable via an opened output stream, request to |
| 4485 | // go via this output stream by adding a second source to the patch |
| 4486 | // description |
| 4487 | output = selectOutput(outputs); |
| 4488 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 4489 | ALOGE("%s no output available for internal patch sink", __func__); |
| 4490 | return INVALID_OPERATION; |
| 4491 | } |
| 4492 | outputDesc = mOutputs.valueFor(output); |
| 4493 | if (outputDesc->isDuplicated()) { |
| 4494 | ALOGV("%s output for device %s is duplicated", |
| 4495 | __func__, sinkDevice->toString().c_str()); |
| 4496 | return INVALID_OPERATION; |
| 4497 | } |
| 4498 | sourceDesc->setSwOutput(outputDesc); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4499 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4500 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 4501 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4502 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 4503 | // - 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] | 4504 | // - called from startAudioSource (aka sourceDesc is not internal) and source device |
| 4505 | // does not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4506 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 4507 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4508 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4509 | (!sourceDesc->isInternal() && |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4510 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4511 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4512 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4513 | return INVALID_OPERATION; |
| 4514 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4515 | sourceDesc->setUseSwBridge(); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4516 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4517 | audio_port_config srcMixPortConfig = {}; |
Ytai Ben-Tsvi | c9d2a91 | 2021-11-22 16:43:09 -0800 | [diff] [blame] | 4518 | outputDesc->toAudioPortConfig(&srcMixPortConfig, nullptr); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4519 | // for volume control, we may need a valid stream |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4520 | srcMixPortConfig.ext.mix.usecase.stream = !sourceDesc->isInternal() ? |
| 4521 | mEngine->getStreamTypeForAttributes(sourceDesc->attributes()) : |
| 4522 | AUDIO_STREAM_PATCH; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4523 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4524 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4525 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4526 | } |
| 4527 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 4528 | |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4529 | installPatch: |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4530 | status_t status = installPatch( |
| 4531 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 4532 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4533 | 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] | 4534 | return INVALID_OPERATION; |
| 4535 | } |
| 4536 | } else { |
| 4537 | return BAD_VALUE; |
| 4538 | } |
| 4539 | } else { |
| 4540 | return BAD_VALUE; |
| 4541 | } |
| 4542 | return NO_ERROR; |
| 4543 | } |
| 4544 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4545 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, uid_t uid) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4546 | { |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4547 | ALOGV("%s patch %d", __func__, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4548 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 4549 | |
| 4550 | if (index < 0) { |
| 4551 | return BAD_VALUE; |
| 4552 | } |
| 4553 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4554 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 4555 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4556 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4557 | return INVALID_OPERATION; |
| 4558 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4559 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 4560 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 4561 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 4562 | if (sourceDesc != nullptr && sourceDesc->getPatchHandle() == handle) { |
| 4563 | portId = sourceDesc->portId(); |
| 4564 | break; |
| 4565 | } |
| 4566 | } |
| 4567 | return portId != AUDIO_PORT_HANDLE_NONE ? |
| 4568 | stopAudioSource(portId) : releaseAudioPatchInternal(handle); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4569 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4570 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4571 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4572 | uint32_t delayMs, |
| 4573 | const sp<SourceClientDescriptor>& sourceDesc) |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4574 | { |
| 4575 | ALOGV("%s patch %d", __func__, handle); |
| 4576 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 4577 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 4578 | return BAD_VALUE; |
| 4579 | } |
| 4580 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4581 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4582 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4583 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4584 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4585 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4586 | 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] | 4587 | return BAD_VALUE; |
| 4588 | } |
| 4589 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4590 | setOutputDevices(outputDesc, |
| 4591 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 4592 | true, |
| 4593 | 0, |
| 4594 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4595 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4596 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4597 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4598 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4599 | 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] | 4600 | return BAD_VALUE; |
| 4601 | } |
| 4602 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4603 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4604 | true, |
| 4605 | NULL); |
| 4606 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4607 | status_t status = |
| 4608 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 4609 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 4610 | __func__, status, patchDesc->getAfHandle()); |
| 4611 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4612 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4613 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4614 | // SW or HW Bridge |
| 4615 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 4616 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4617 | 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] | 4618 | outputDesc = mOutputs.getOutputFromId(patch->sources[1].id); |
| 4619 | } else if (patch->num_sources == 1 && sourceDesc != nullptr) { |
| 4620 | outputDesc = sourceDesc->swOutput().promote(); |
| 4621 | } |
| 4622 | if (outputDesc == nullptr) { |
| 4623 | ALOGW("%s no output for id %d", __func__, patch->sources[0].id); |
| 4624 | // releaseOutput has already called closeOutput in case of direct output |
| 4625 | return NO_ERROR; |
| 4626 | } |
| 4627 | if (!outputDesc->isActive() && !sourceDesc->useSwBridge()) { |
| 4628 | resetOutputDevice(outputDesc); |
| 4629 | } else { |
| 4630 | // Reuse patch handle if still valid / do not force rerouting if still routed |
| 4631 | patchHandle = outputDesc->getPatchHandle(); |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4632 | setOutputDevices(outputDesc, |
| 4633 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4634 | patchHandle == AUDIO_PATCH_HANDLE_NONE, /*force*/ |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4635 | 0, |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4636 | patchHandle == AUDIO_PATCH_HANDLE_NONE ? nullptr : &patchHandle); |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4637 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4638 | } else { |
| 4639 | return BAD_VALUE; |
| 4640 | } |
| 4641 | } else { |
| 4642 | return BAD_VALUE; |
| 4643 | } |
| 4644 | return NO_ERROR; |
| 4645 | } |
| 4646 | |
| 4647 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 4648 | struct audio_patch *patches, |
| 4649 | unsigned int *generation) |
| 4650 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4651 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4652 | return BAD_VALUE; |
| 4653 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4654 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4655 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4656 | } |
| 4657 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4658 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4659 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4660 | ALOGV("setAudioPortConfig()"); |
| 4661 | |
| 4662 | if (config == NULL) { |
| 4663 | return BAD_VALUE; |
| 4664 | } |
| 4665 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 4666 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4667 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 4668 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4671 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4672 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 4673 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4674 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4675 | if (outputDesc == NULL) { |
| 4676 | return BAD_VALUE; |
| 4677 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4678 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 4679 | "setAudioPortConfig() called on duplicated output %d", |
| 4680 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4681 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4682 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4683 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4684 | if (inputDesc == NULL) { |
| 4685 | return BAD_VALUE; |
| 4686 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4687 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4688 | } else { |
| 4689 | return BAD_VALUE; |
| 4690 | } |
| 4691 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 4692 | sp<DeviceDescriptor> deviceDesc; |
| 4693 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 4694 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 4695 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 4696 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 4697 | } else { |
| 4698 | return BAD_VALUE; |
| 4699 | } |
| 4700 | if (deviceDesc == NULL) { |
| 4701 | return BAD_VALUE; |
| 4702 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4703 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4704 | } else { |
| 4705 | return BAD_VALUE; |
| 4706 | } |
| 4707 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4708 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4709 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 4710 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4711 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4712 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 4713 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4714 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4715 | if (status != NO_ERROR) { |
| 4716 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4717 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4718 | |
| 4719 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4720 | } |
| 4721 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4722 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 4723 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4724 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4725 | clearAudioPatches(uid); |
| 4726 | clearSessionRoutes(uid); |
| 4727 | } |
| 4728 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4729 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 4730 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4731 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4732 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4733 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4734 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4735 | } |
| 4736 | } |
| 4737 | } |
| 4738 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4739 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4740 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4741 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 4742 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 4743 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 4744 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4745 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4746 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 4747 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 4748 | continue; |
| 4749 | } |
| 4750 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4751 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4752 | continue; |
| 4753 | } |
| 4754 | // If the default device for this strategy is on another output mix, |
| 4755 | // invalidate all tracks in this strategy to force re connection. |
| 4756 | // Otherwise select new device on the output mix. |
| 4757 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4758 | for (auto stream : mEngine->getStreamTypesForProductStrategy(ps)) { |
| 4759 | mpClientInterface->invalidateStream(stream); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4760 | } |
| 4761 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4762 | setOutputDevices( |
| 4763 | outputDesc, getNewOutputDevices(outputDesc, false /*fromCache*/), false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 4769 | { |
| 4770 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4771 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4772 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4773 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4774 | for (const auto& client : outputDesc->getClientIterable()) { |
| 4775 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 4776 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4777 | auto clientStrategy = client->strategy(); |
| 4778 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 4779 | end(affectedStrategies)) { |
| 4780 | continue; |
| 4781 | } |
| 4782 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4783 | } |
| 4784 | } |
| 4785 | } |
| 4786 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4787 | for (const auto& strategy : affectedStrategies) { |
| 4788 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4789 | } |
| 4790 | |
| 4791 | // remove input routes associated with this uid |
| 4792 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4793 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4794 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4795 | for (const auto& client : inputDesc->getClientIterable()) { |
| 4796 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 4797 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 4798 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | // reroute inputs if necessary |
| 4803 | SortedVector<audio_io_handle_t> inputsToClose; |
| 4804 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4805 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 4806 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4807 | inputsToClose.add(inputDesc->mIoHandle); |
| 4808 | } |
| 4809 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4810 | for (const auto& input : inputsToClose) { |
| 4811 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4815 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 4816 | { |
| 4817 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4818 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 4819 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4820 | stopAudioSource(mAudioSources.keyAt(i)); |
| 4821 | } |
| 4822 | } |
| 4823 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4824 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4825 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 4826 | audio_io_handle_t *ioHandle, |
| 4827 | audio_devices_t *device) |
| 4828 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 4829 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 4830 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4831 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4832 | *device = mEngine->getInputDeviceForAttributes(attr)->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4833 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 4834 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4835 | } |
| 4836 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4837 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4838 | const audio_attributes_t *attributes, |
| 4839 | audio_port_handle_t *portId, |
| 4840 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4841 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4842 | ALOGV("%s", __FUNCTION__); |
| 4843 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 4844 | |
| 4845 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 4846 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 4847 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4848 | return BAD_VALUE; |
| 4849 | } |
| 4850 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4851 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 4852 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4853 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 4854 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4855 | return INVALID_OPERATION; |
| 4856 | } |
| 4857 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4858 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4859 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4860 | String8(source->ext.device.address), |
| 4861 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4862 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4863 | 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] | 4864 | return BAD_VALUE; |
| 4865 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4866 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 4867 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4868 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4869 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4870 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4871 | mEngine->getStreamTypeForAttributes(*attributes), |
| 4872 | mEngine->getProductStrategyForAttributes(*attributes), |
| 4873 | toVolumeSource(*attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4874 | |
| 4875 | status_t status = connectAudioSource(sourceDesc); |
| 4876 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4877 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4878 | } |
| 4879 | return status; |
| 4880 | } |
| 4881 | |
Francois Gaffie | 601801d | 2021-06-22 13:27:39 +0200 | [diff] [blame] | 4882 | sp<SourceClientDescriptor> AudioPolicyManager::startAudioSourceInternal( |
| 4883 | const struct audio_port_config *source, const audio_attributes_t *attributes, uid_t uid) |
| 4884 | { |
| 4885 | ALOGV("%s", __FUNCTION__); |
| 4886 | audio_port_handle_t portId = AUDIO_PORT_HANDLE_NONE; |
| 4887 | |
| 4888 | status_t status = startAudioSource(source, attributes, &portId, uid); |
| 4889 | ALOGE_IF(status != OK, "%s: failed to start audio source (%d)", __func__, status); |
| 4890 | return mAudioSources.valueFor(portId); |
| 4891 | } |
| 4892 | |
| 4893 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4894 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4895 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4896 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4897 | |
| 4898 | // make sure we only have one patch per source. |
| 4899 | disconnectAudioSource(sourceDesc); |
| 4900 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4901 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4902 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 4903 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 4904 | sourceDesc->srcDevice()->type(), |
| 4905 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 4906 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4907 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 4908 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4909 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4910 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4911 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 4912 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 4913 | return INVALID_OPERATION; |
| 4914 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4915 | PatchBuilder patchBuilder; |
| 4916 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 4917 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4918 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 4919 | return connectAudioSourceToSink( |
| 4920 | sourceDesc, sinkDevice, patchBuilder.patch(), handle, mUidCached, 0 /*delayMs*/); |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4921 | } |
| 4922 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4923 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4924 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4925 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 4926 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4927 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4928 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4929 | return BAD_VALUE; |
| 4930 | } |
| 4931 | status_t status = disconnectAudioSource(sourceDesc); |
| 4932 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4933 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4934 | return status; |
| 4935 | } |
| 4936 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4937 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 4938 | { |
| 4939 | if (mMasterMono == mono) { |
| 4940 | return NO_ERROR; |
| 4941 | } |
| 4942 | mMasterMono = mono; |
| 4943 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 4944 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 4945 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 4946 | // |
| 4947 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 4948 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 4949 | // play back offloaded. |
| 4950 | if (mMasterMono) { |
| 4951 | Vector<audio_io_handle_t> offloaded; |
| 4952 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 4953 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4954 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 4955 | offloaded.push(desc->mIoHandle); |
| 4956 | } |
| 4957 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4958 | for (const auto& handle : offloaded) { |
| 4959 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4960 | } |
| 4961 | } |
| 4962 | // update master mono for all remaining outputs |
| 4963 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 4964 | updateMono(mOutputs.keyAt(i)); |
| 4965 | } |
| 4966 | return NO_ERROR; |
| 4967 | } |
| 4968 | |
| 4969 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 4970 | { |
| 4971 | *mono = mMasterMono; |
| 4972 | return NO_ERROR; |
| 4973 | } |
| 4974 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 4975 | float AudioPolicyManager::getStreamVolumeDB( |
| 4976 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 4977 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 4978 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 4979 | } |
| 4980 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4981 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 4982 | audio_format_t *surroundFormats, |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4983 | bool *surroundFormatsEnabled) |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4984 | { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4985 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && |
| 4986 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4987 | return BAD_VALUE; |
| 4988 | } |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4989 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 4990 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4991 | |
| 4992 | size_t formatsWritten = 0; |
| 4993 | size_t formatsMax = *numSurroundFormats; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 4994 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4995 | *numSurroundFormats = mConfig.getSurroundFormats().size(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4996 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 4997 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4998 | for (const auto& format: mConfig.getSurroundFormats()) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4999 | if (formatsWritten < formatsMax) { |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5000 | surroundFormats[formatsWritten] = format.first; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5001 | bool formatEnabled = true; |
| 5002 | switch (forceUse) { |
| 5003 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5004 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5005 | break; |
| 5006 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 5007 | formatEnabled = false; |
| 5008 | break; |
| 5009 | default: // AUTO or ALWAYS => true |
| 5010 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5011 | } |
| 5012 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 5013 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5014 | } |
| 5015 | return NO_ERROR; |
| 5016 | } |
| 5017 | |
Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 5018 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, |
| 5019 | audio_format_t *surroundFormats) { |
| 5020 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { |
| 5021 | return BAD_VALUE; |
| 5022 | } |
| 5023 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", |
| 5024 | __func__, *numSurroundFormats, surroundFormats); |
| 5025 | |
| 5026 | size_t formatsWritten = 0; |
| 5027 | size_t formatsMax = *numSurroundFormats; |
| 5028 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
| 5029 | |
| 5030 | // Return formats from all device profiles that have already been resolved by |
| 5031 | // checkOutputsForDevice(). |
| 5032 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 5033 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 5034 | audio_devices_t deviceType = device->type(); |
| 5035 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 5036 | // returns formats reported by HDMI devices. |
| 5037 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 5038 | continue; |
| 5039 | } |
| 5040 | // Formats reported by sink devices |
| 5041 | std::unordered_set<audio_format_t> formatset; |
| 5042 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 5043 | formatset.insert(it->second.begin(), it->second.end()); |
| 5044 | } |
| 5045 | |
| 5046 | // Formats hard-coded in the in policy configuration file (if any). |
| 5047 | FormatVector encodedFormats = device->encodedFormats(); |
| 5048 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 5049 | // Filter the formats which are supported by the vendor hardware. |
| 5050 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
| 5051 | if (mConfig.getSurroundFormats().count(*it) != 0) { |
| 5052 | formats.insert(*it); |
| 5053 | } else { |
| 5054 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5055 | if (pair.second.count(*it) != 0) { |
| 5056 | formats.insert(pair.first); |
| 5057 | break; |
| 5058 | } |
| 5059 | } |
| 5060 | } |
| 5061 | } |
| 5062 | } |
| 5063 | *numSurroundFormats = formats.size(); |
| 5064 | for (const auto& format: formats) { |
| 5065 | if (formatsWritten < formatsMax) { |
| 5066 | surroundFormats[formatsWritten++] = format; |
| 5067 | } |
| 5068 | } |
| 5069 | return NO_ERROR; |
| 5070 | } |
| 5071 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5072 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 5073 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5074 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5075 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 5076 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5077 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5078 | return BAD_VALUE; |
| 5079 | } |
| 5080 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5081 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 5082 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5083 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5084 | return INVALID_OPERATION; |
| 5085 | } |
| 5086 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5087 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5088 | return NO_ERROR; |
| 5089 | } |
| 5090 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5091 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5092 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5093 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5094 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5095 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5096 | } |
| 5097 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5098 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 5099 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5100 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | sp<SwAudioOutputDescriptor> outputDesc; |
| 5105 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5106 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 5107 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5108 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 5109 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5110 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5111 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5112 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5113 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5114 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5115 | name.c_str(), |
| 5116 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5117 | if (status != NO_ERROR) { |
| 5118 | continue; |
| 5119 | } |
| 5120 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 5121 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5122 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5123 | name.c_str(), |
| 5124 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5125 | profileUpdated |= (status == NO_ERROR); |
| 5126 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5127 | // 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] | 5128 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5129 | AUDIO_DEVICE_IN_HDMI); |
| 5130 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 5131 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5132 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5133 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5134 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5135 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 5136 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5137 | name.c_str(), |
| 5138 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5139 | if (status != NO_ERROR) { |
| 5140 | continue; |
| 5141 | } |
| 5142 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 5143 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 5144 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5145 | name.c_str(), |
| 5146 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5147 | profileUpdated |= (status == NO_ERROR); |
| 5148 | } |
| 5149 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5150 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 5151 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5152 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5153 | } |
| 5154 | |
| 5155 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 5156 | } |
| 5157 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5158 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5159 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5160 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 5161 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 5162 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 5163 | } |
| 5164 | } |
| 5165 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 5166 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 5167 | { |
| 5168 | for (const auto& hwModule : mHwModules) { |
| 5169 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5170 | for (const auto &outProfile : outputProfiles) { |
| 5171 | struct audio_port audioPort; |
| 5172 | outProfile->toAudioPort(&audioPort); |
| 5173 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 5174 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 5175 | return true; |
| 5176 | } |
| 5177 | } |
| 5178 | } |
| 5179 | } |
| 5180 | return false; |
| 5181 | } |
| 5182 | |
Carter Hsu | 325a8eb | 2022-01-19 19:56:51 +0800 | [diff] [blame] | 5183 | bool AudioPolicyManager::isUltrasoundSupported() |
| 5184 | { |
| 5185 | bool hasUltrasoundOutput = false; |
| 5186 | bool hasUltrasoundInput = false; |
| 5187 | for (const auto& hwModule : mHwModules) { |
| 5188 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 5189 | if (!hasUltrasoundOutput) { |
| 5190 | for (const auto &outProfile : outputProfiles) { |
| 5191 | if (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) { |
| 5192 | hasUltrasoundOutput = true; |
| 5193 | break; |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | const InputProfileCollection &inputProfiles = hwModule->getInputProfiles(); |
| 5199 | if (!hasUltrasoundInput) { |
| 5200 | for (const auto &inputProfile : inputProfiles) { |
| 5201 | if (inputProfile->getFlags() & AUDIO_INPUT_FLAG_ULTRASOUND) { |
| 5202 | hasUltrasoundInput = true; |
| 5203 | break; |
| 5204 | } |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | if (hasUltrasoundOutput && hasUltrasoundInput) |
| 5209 | return true; |
| 5210 | } |
| 5211 | return false; |
| 5212 | } |
| 5213 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 5214 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 5215 | { |
| 5216 | return getConfig().isCallScreenModeSupported(); |
| 5217 | } |
| 5218 | |
| 5219 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5220 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5221 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5222 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5223 | if (!sourceDesc->isConnected()) { |
| 5224 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 5225 | return NO_ERROR; |
| 5226 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5227 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 5228 | if (swOutput != 0) { |
| 5229 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5230 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5231 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5232 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5233 | if (releaseOutput(sourceDesc->portId())) { |
| 5234 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 5235 | // no need to release audio patch here but just return NO_ERROR. |
| 5236 | return NO_ERROR; |
| 5237 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5238 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5239 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5240 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5241 | // close Hwoutput and remove from mHwOutputs |
| 5242 | } else { |
| 5243 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 5244 | } |
| 5245 | } |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 5246 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle(), 0, sourceDesc); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 5247 | sourceDesc->disconnect(); |
| 5248 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5251 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 5252 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5253 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5254 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5255 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5256 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5257 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5258 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 5259 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5260 | source = sourceDesc; |
| 5261 | break; |
| 5262 | } |
| 5263 | } |
| 5264 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 5265 | } |
| 5266 | |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5267 | /* static */ |
| 5268 | bool AudioPolicyManager::isChannelMaskSpatialized(audio_channel_mask_t channels) { |
| 5269 | switch (channels) { |
| 5270 | case AUDIO_CHANNEL_OUT_5POINT1: |
| 5271 | case AUDIO_CHANNEL_OUT_5POINT1POINT2: |
| 5272 | case AUDIO_CHANNEL_OUT_5POINT1POINT4: |
| 5273 | case AUDIO_CHANNEL_OUT_7POINT1: |
| 5274 | case AUDIO_CHANNEL_OUT_7POINT1POINT2: |
| 5275 | case AUDIO_CHANNEL_OUT_7POINT1POINT4: |
| 5276 | return true; |
| 5277 | default: |
| 5278 | return false; |
| 5279 | } |
| 5280 | } |
| 5281 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5282 | bool AudioPolicyManager::canBeSpatializedInt(const audio_attributes_t *attr, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5283 | const audio_config_t *config, |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5284 | const AudioDeviceTypeAddrVector &devices, |
| 5285 | bool allowCurrentOutputReconfig) const |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5286 | { |
| 5287 | // The caller can have the audio attributes criteria ignored by either passing a null ptr or |
| 5288 | // the AUDIO_ATTRIBUTES_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5289 | // 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] | 5290 | // and game usages. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5291 | if (attr != nullptr && *attr != AUDIO_ATTRIBUTES_INITIALIZER) { |
| 5292 | if (attr->usage != AUDIO_USAGE_MEDIA && attr->usage != AUDIO_USAGE_GAME) { |
| 5293 | return false; |
| 5294 | } |
| 5295 | if ((attr->flags & (AUDIO_FLAG_CONTENT_SPATIALIZED | AUDIO_FLAG_NEVER_SPATIALIZE)) != 0) { |
| 5296 | return false; |
| 5297 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5298 | } |
| 5299 | |
| 5300 | // The caller can have the devices criteria ignored by passing and empty vector, and |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5301 | // getSpatializerOutputProfile() will ignore the devices when looking for a match. |
| 5302 | // Otherwise an output profile supporting a spatializer effect that can be routed |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5303 | // to the specified devices must exist. |
| 5304 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5305 | getSpatializerOutputProfile(config, devices); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5306 | if (profile == nullptr) { |
| 5307 | return false; |
| 5308 | } |
| 5309 | |
| 5310 | // The caller can have the audio config criteria ignored by either passing a null ptr or |
| 5311 | // the AUDIO_CONFIG_INITIALIZER value. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5312 | // 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] | 5313 | // some positional channel masks. |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5314 | // If the spatializer output is already opened, only channel masks included in the |
| 5315 | // spatializer output mixer channel mask are allowed. |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5316 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5317 | if (config != nullptr && *config != AUDIO_CONFIG_INITIALIZER) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5318 | if (!isChannelMaskSpatialized(config->channel_mask)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5319 | return false; |
| 5320 | } |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5321 | if (!allowCurrentOutputReconfig && mSpatializerOutput != nullptr |
| 5322 | && mSpatializerOutput->mProfile == profile) { |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5323 | if ((config->channel_mask & mSpatializerOutput->mMixerChannelMask) |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5324 | != config->channel_mask) { |
| 5325 | return false; |
| 5326 | } |
| 5327 | } |
| 5328 | } |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5329 | return true; |
| 5330 | } |
| 5331 | |
| 5332 | void AudioPolicyManager::checkVirtualizerClientRoutes() { |
| 5333 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 5334 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5335 | const sp<SwAudioOutputDescriptor>& desc = mOutputs[i]; |
| 5336 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5337 | audio_attributes_t attr = client->attributes(); |
| 5338 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 5339 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5340 | audio_config_base_t clientConfig = client->config(); |
| 5341 | audio_config_t config = audio_config_initializer(&clientConfig); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5342 | if (desc != mSpatializerOutput |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5343 | && canBeSpatializedInt(&attr, &config, |
| 5344 | devicesTypeAddress, false /* allowCurrentOutputReconfig */)) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5345 | streamsToInvalidate.insert(client->stream()); |
| 5346 | } |
| 5347 | } |
| 5348 | } |
| 5349 | |
| 5350 | for (audio_stream_type_t stream : streamsToInvalidate) { |
| 5351 | mpClientInterface->invalidateStream(stream); |
| 5352 | } |
| 5353 | } |
| 5354 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5355 | status_t AudioPolicyManager::getSpatializerOutput(const audio_config_base_t *mixerConfig, |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5356 | const audio_attributes_t *attr, |
| 5357 | audio_io_handle_t *output) { |
| 5358 | *output = AUDIO_IO_HANDLE_NONE; |
| 5359 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5360 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(*attr, nullptr, false); |
| 5361 | AudioDeviceTypeAddrVector devicesTypeAddress = devices.toTypeAddrVector(); |
| 5362 | audio_config_t *configPtr = nullptr; |
| 5363 | audio_config_t config; |
| 5364 | if (mixerConfig != nullptr) { |
| 5365 | config = audio_config_initializer(mixerConfig); |
| 5366 | configPtr = &config; |
| 5367 | } |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5368 | if (!canBeSpatializedInt( |
| 5369 | attr, configPtr, devicesTypeAddress)) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5370 | ALOGW("%s provided attributes or mixer config cannot be spatialized", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5371 | return BAD_VALUE; |
| 5372 | } |
| 5373 | |
| 5374 | sp<IOProfile> profile = |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5375 | getSpatializerOutputProfile(configPtr, devicesTypeAddress); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5376 | if (profile == nullptr) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5377 | ALOGW("%s no suitable output profile for provided attributes or mixer config", __func__); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5378 | return BAD_VALUE; |
| 5379 | } |
| 5380 | |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5381 | if (mSpatializerOutput != nullptr && mSpatializerOutput->mProfile == profile |
| 5382 | && configPtr != nullptr |
| 5383 | && configPtr->channel_mask == mSpatializerOutput->mMixerChannelMask) { |
| 5384 | *output = mSpatializerOutput->mIoHandle; |
| 5385 | ALOGV("%s returns current spatializer output %d", __func__, *output); |
| 5386 | return NO_ERROR; |
| 5387 | } |
| 5388 | mSpatializerOutput.clear(); |
| 5389 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5390 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5391 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5392 | ALOGV("%s found output %d for spatializer profile", __func__, desc->mIoHandle); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5393 | mSpatializerOutput = desc; |
| 5394 | break; |
| 5395 | } |
| 5396 | } |
| 5397 | if (mSpatializerOutput == nullptr) { |
| 5398 | ALOGW("%s no opened spatializer output for profile %s", |
| 5399 | __func__, profile->getName().c_str()); |
| 5400 | return BAD_VALUE; |
| 5401 | } |
| 5402 | |
| 5403 | if (configPtr != nullptr |
| 5404 | && configPtr->channel_mask != mSpatializerOutput->mMixerChannelMask) { |
| 5405 | audio_config_base_t savedMixerConfig = { |
| 5406 | .sample_rate = mSpatializerOutput->getSamplingRate(), |
| 5407 | .format = mSpatializerOutput->getFormat(), |
| 5408 | .channel_mask = mSpatializerOutput->mMixerChannelMask, |
| 5409 | }; |
| 5410 | DeviceVector savedDevices = mSpatializerOutput->devices(); |
| 5411 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5412 | ALOGV("%s reopening spatializer output to match channel mask %#x (current mask %#x)", |
| 5413 | __func__, configPtr->channel_mask, mSpatializerOutput->mMixerChannelMask); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5414 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5415 | closeOutput(mSpatializerOutput->mIoHandle); |
| 5416 | //from now on mSpatializerOutput is null |
| 5417 | |
| 5418 | sp<SwAudioOutputDescriptor> desc = |
| 5419 | openOutputWithProfileAndDevice(profile, devices, mixerConfig); |
| 5420 | if (desc == nullptr) { |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5421 | // re open the spatializer output with previous channel mask |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5422 | desc = openOutputWithProfileAndDevice(profile, savedDevices, &savedMixerConfig); |
| 5423 | if (desc == nullptr) { |
| 5424 | ALOGE("%s failed to restore mSpatializerOutput with previous config", __func__); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5425 | } else { |
| 5426 | mSpatializerOutput = desc; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5427 | } |
| 5428 | mPreviousOutputs = mOutputs; |
| 5429 | mpClientInterface->onAudioPortListUpdate(); |
| 5430 | *output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 5431 | ALOGW("%s could not open spatializer output with requested config", __func__); |
| 5432 | return BAD_VALUE; |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5433 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5434 | mSpatializerOutput = desc; |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5435 | mPreviousOutputs = mOutputs; |
| 5436 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5437 | } |
| 5438 | |
| 5439 | checkVirtualizerClientRoutes(); |
| 5440 | |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5441 | *output = mSpatializerOutput->mIoHandle; |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5442 | ALOGV("%s returns new spatializer output %d", __func__, *output); |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5443 | return NO_ERROR; |
| 5444 | } |
| 5445 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5446 | status_t AudioPolicyManager::releaseSpatializerOutput(audio_io_handle_t output) { |
| 5447 | if (mSpatializerOutput == nullptr) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5448 | return INVALID_OPERATION; |
| 5449 | } |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5450 | if (mSpatializerOutput->mIoHandle != output) { |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5451 | return BAD_VALUE; |
| 5452 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5453 | |
Eric Laurent | fa0f674 | 2021-08-17 18:39:44 +0200 | [diff] [blame] | 5454 | mSpatializerOutput.clear(); |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5455 | |
| 5456 | checkVirtualizerClientRoutes(); |
| 5457 | |
Eric Laurent | cad6c0d | 2021-07-13 15:12:39 +0200 | [diff] [blame] | 5458 | return NO_ERROR; |
| 5459 | } |
| 5460 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5461 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5462 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5463 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5464 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 5465 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 5466 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5467 | } |
| 5468 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5469 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5470 | if (std::string audioPolicyXmlConfigFile = audio_get_audio_policy_config_file(); |
| 5471 | !audioPolicyXmlConfigFile.empty()) { |
| 5472 | status_t ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile.c_str(), &config); |
| 5473 | if (ret == NO_ERROR) { |
| 5474 | config.setSource(audioPolicyXmlConfigFile); |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 5475 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5476 | return ret; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 5477 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 5478 | return BAD_VALUE; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5479 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 5480 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5481 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 5482 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5483 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 5484 | 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] | 5485 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5486 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5487 | mA2dpSuspended(false), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5488 | mConfig(mHwModulesAll, mOutputDevicesAll, mInputDevicesAll, mDefaultOutputDevice), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5489 | mAudioPortGeneration(1), |
| 5490 | mBeaconMuteRefCount(0), |
| 5491 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5492 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5493 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5494 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 5495 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5496 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5497 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5498 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5499 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 5500 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 5501 | { |
| 5502 | loadConfig(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5503 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5504 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 5505 | void AudioPolicyManager::loadConfig() { |
| 5506 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5507 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5508 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5509 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 5513 | { |
| 5514 | auto engLib = EngineLibrary::load( |
| 5515 | "libaudiopolicyengine" + getConfig().getEngineLibraryNameSuffix() + ".so"); |
| 5516 | if (!engLib) { |
| 5517 | ALOGE("%s: Failed to load the engine library", __FUNCTION__); |
| 5518 | return NO_INIT; |
| 5519 | } |
| 5520 | mEngine = engLib->createEngine(); |
| 5521 | if (mEngine == nullptr) { |
| 5522 | ALOGE("%s: Failed to instantiate the APM engine", __FUNCTION__); |
| 5523 | return NO_INIT; |
| 5524 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5525 | } |
| 5526 | mEngine->setObserver(this); |
| 5527 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5528 | if (status != NO_ERROR) { |
| 5529 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 5530 | return status; |
| 5531 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5532 | |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 5533 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 5534 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 5535 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5536 | // after parsing the config, mOutputDevicesAll and mInputDevicesAll contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5537 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5538 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5539 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5540 | // make sure default device is reachable |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5541 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { |
| 5542 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", |
| 5543 | mDefaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5544 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5545 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5546 | // If microphones address is empty, set it according to device type |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 5547 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5548 | if (mAvailableInputDevices[i]->address().empty()) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5549 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5550 | mAvailableInputDevices[i]->setAddress(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5551 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5552 | mAvailableInputDevices[i]->setAddress(AUDIO_BACK_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 5553 | } |
| 5554 | } |
| 5555 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5556 | |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5557 | 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] | 5558 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 5559 | // Silence ALOGV statements |
| 5560 | property_set("log.tag." LOG_TAG, "D"); |
| 5561 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5562 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 5563 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5564 | } |
| 5565 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5566 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5567 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5568 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5569 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5570 | } |
| 5571 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5572 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5573 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5574 | mAvailableOutputDevices.clear(); |
| 5575 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5576 | mOutputs.clear(); |
| 5577 | mInputs.clear(); |
| 5578 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 5579 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5580 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5581 | } |
| 5582 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5583 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5584 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5585 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5586 | } |
| 5587 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5588 | // --- |
| 5589 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5590 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 5591 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5592 | DeviceVector newDevices; |
| 5593 | onNewAudioModulesAvailableInt(&newDevices); |
| 5594 | if (!newDevices.empty()) { |
| 5595 | nextAudioPortGeneration(); |
| 5596 | mpClientInterface->onAudioPortListUpdate(); |
| 5597 | } |
| 5598 | } |
| 5599 | |
| 5600 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 5601 | { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5602 | for (const auto& hwModule : mHwModulesAll) { |
| 5603 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 5604 | continue; |
| 5605 | } |
| 5606 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
| 5607 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 5608 | ALOGW("could not open HW module %s", hwModule->getName()); |
| 5609 | continue; |
| 5610 | } |
| 5611 | mHwModules.push_back(hwModule); |
Dean Wheatley | 12a8713 | 2021-04-16 10:08:49 +1000 | [diff] [blame] | 5612 | // open all output streams needed to access attached devices. |
| 5613 | // direct outputs are closed immediately after checking the availability of attached devices |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5614 | // This also validates mAvailableOutputDevices list |
| 5615 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 5616 | if (!outProfile->canOpenNewIo()) { |
| 5617 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 5618 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 5619 | continue; |
| 5620 | } |
| 5621 | if (!outProfile->hasSupportedDevices()) { |
| 5622 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 5623 | continue; |
| 5624 | } |
Carter Hsu | 1a3364a | 2022-01-21 15:32:56 +0800 | [diff] [blame] | 5625 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0 || |
| 5626 | (outProfile->getFlags() & AUDIO_OUTPUT_FLAG_ULTRASOUND) != 0) { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5627 | mTtsOutputAvailable = true; |
| 5628 | } |
| 5629 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5630 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 5631 | DeviceVector availProfileDevices = supportedDevices.filter(mOutputDevicesAll); |
| 5632 | sp<DeviceDescriptor> supportedDevice = 0; |
| 5633 | if (supportedDevices.contains(mDefaultOutputDevice)) { |
| 5634 | supportedDevice = mDefaultOutputDevice; |
| 5635 | } else { |
| 5636 | // choose first device present in profile's SupportedDevices also part of |
| 5637 | // mAvailableOutputDevices. |
| 5638 | if (availProfileDevices.isEmpty()) { |
| 5639 | continue; |
| 5640 | } |
| 5641 | supportedDevice = availProfileDevices.itemAt(0); |
| 5642 | } |
| 5643 | if (!mOutputDevicesAll.contains(supportedDevice)) { |
| 5644 | continue; |
| 5645 | } |
| 5646 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 5647 | mpClientInterface); |
| 5648 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 5649 | status_t status = outputDesc->open(nullptr /* halConfig */, nullptr /* mixerConfig */, |
| 5650 | DeviceVector(supportedDevice), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5651 | AUDIO_STREAM_DEFAULT, |
| 5652 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 5653 | if (status != NO_ERROR) { |
| 5654 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 5655 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 5656 | continue; |
| 5657 | } |
| 5658 | for (const auto &device : availProfileDevices) { |
| 5659 | // give a valid ID to an attached device once confirmed it is reachable |
| 5660 | if (!device->isAttached()) { |
| 5661 | device->attach(hwModule); |
| 5662 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 5663 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5664 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5665 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 5666 | } |
| 5667 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5668 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5669 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 5670 | mPrimaryOutput = outputDesc; |
| 5671 | } |
Eric Laurent | 3909598 | 2021-08-24 18:29:27 +0200 | [diff] [blame] | 5672 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5673 | outputDesc->close(); |
| 5674 | } else { |
| 5675 | addOutput(output, outputDesc); |
| 5676 | setOutputDevices(outputDesc, |
| 5677 | DeviceVector(supportedDevice), |
| 5678 | true, |
| 5679 | 0, |
| 5680 | NULL); |
| 5681 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5682 | } |
| 5683 | // open input streams needed to access attached devices to validate |
| 5684 | // mAvailableInputDevices list |
| 5685 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 5686 | if (!inProfile->canOpenNewIo()) { |
| 5687 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 5688 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 5689 | continue; |
| 5690 | } |
| 5691 | if (!inProfile->hasSupportedDevices()) { |
| 5692 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 5693 | continue; |
| 5694 | } |
| 5695 | // chose first device present in profile's SupportedDevices also part of |
| 5696 | // available input devices |
| 5697 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 5698 | DeviceVector availProfileDevices = supportedDevices.filter(mInputDevicesAll); |
| 5699 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 5700 | ALOGV("%s: Input device list is empty! for profile %s", |
| 5701 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5702 | continue; |
| 5703 | } |
| 5704 | sp<AudioInputDescriptor> inputDesc = |
| 5705 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 5706 | |
| 5707 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 5708 | status_t status = inputDesc->open(nullptr, |
| 5709 | availProfileDevices.itemAt(0), |
| 5710 | AUDIO_SOURCE_MIC, |
| 5711 | AUDIO_INPUT_FLAG_NONE, |
| 5712 | &input); |
| 5713 | if (status != NO_ERROR) { |
| 5714 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 5715 | availProfileDevices.toString().c_str(), |
| 5716 | hwModule->getName()); |
| 5717 | continue; |
| 5718 | } |
| 5719 | for (const auto &device : availProfileDevices) { |
| 5720 | // give a valid ID to an attached device once confirmed it is reachable |
| 5721 | if (!device->isAttached()) { |
| 5722 | device->attach(hwModule); |
| 5723 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 5724 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5725 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5726 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 5727 | } |
| 5728 | } |
| 5729 | inputDesc->close(); |
| 5730 | } |
| 5731 | } |
| 5732 | } |
| 5733 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5734 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 5735 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5736 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5737 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5738 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5739 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5740 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5741 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5742 | } |
| 5743 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5744 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 5745 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5746 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 5747 | ALOGV("%s: removing primary output", __func__); |
| 5748 | mPrimaryOutput = nullptr; |
| 5749 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5750 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5751 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5752 | } |
| 5753 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5754 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 5755 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5756 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5757 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5758 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5759 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5760 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5761 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5762 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5763 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5764 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5765 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5766 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5767 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5768 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5769 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5770 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5771 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5772 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5773 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5774 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 5775 | // first call getAudioPort to get the supported attributes from the HAL |
| 5776 | struct audio_port_v7 port = {}; |
| 5777 | device->toAudioPort(&port); |
| 5778 | status_t status = mpClientInterface->getAudioPort(&port); |
| 5779 | if (status == NO_ERROR) { |
| 5780 | device->importAudioPort(port); |
| 5781 | } |
| 5782 | |
| 5783 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5784 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5785 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5786 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5787 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5788 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 5789 | mOutputs.keyAt(i), device->toString().c_str()); |
| 5790 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5791 | } |
| 5792 | } |
| 5793 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5794 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5795 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5796 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 5797 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5798 | if (profile->supportsDevice(device)) { |
| 5799 | profiles.add(profile); |
| 5800 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 5801 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5802 | } |
| 5803 | } |
| 5804 | } |
| 5805 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 5806 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5807 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5808 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5809 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5810 | return BAD_VALUE; |
| 5811 | } |
| 5812 | |
| 5813 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 5814 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 5815 | 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] | 5816 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5817 | |
| 5818 | // nothing to do if one output is already opened for this profile |
| 5819 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5820 | for (j = 0; j < outputs.size(); j++) { |
| 5821 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5822 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5823 | // matching profile: save the sample rates, format and channel masks supported |
| 5824 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5825 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5826 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5827 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5828 | break; |
| 5829 | } |
| 5830 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5831 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5832 | continue; |
| 5833 | } |
| 5834 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5835 | if (!profile->canOpenNewIo()) { |
| 5836 | ALOGW("Max Output number %u already opened for this profile %s", |
| 5837 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 5838 | continue; |
| 5839 | } |
| 5840 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 5841 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5842 | deviceType, address.string(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5843 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 5844 | 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] | 5845 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5846 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5847 | profiles.removeAt(profile_index); |
| 5848 | profile_index--; |
| 5849 | } else { |
| 5850 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5851 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5852 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5853 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 5854 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5855 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5856 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5857 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5858 | if (device_distinguishes_on_address(deviceType)) { |
| 5859 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 5860 | device->toString().c_str()); |
| 5861 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 5862 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5863 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5864 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 5865 | } |
| 5866 | } |
| 5867 | |
| 5868 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5869 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5870 | return BAD_VALUE; |
| 5871 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5872 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5873 | // check if one opened output is not needed any more after disconnecting one device |
| 5874 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5875 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5876 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5877 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5878 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
Francois Gaffie | c7d4c22 | 2021-12-02 11:12:52 +0100 | [diff] [blame] | 5879 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5880 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5881 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5882 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 5883 | mOutputs.keyAt(i)); |
| 5884 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5885 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5886 | } |
| 5887 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5888 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5889 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5890 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 5891 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5892 | if (!profile->supportsDevice(device)) { |
| 5893 | continue; |
| 5894 | } |
| 5895 | ALOGV("checkOutputsForDevice(): " |
| 5896 | "clearing direct output profile %zu on module %s", |
| 5897 | j, hwModule->getName()); |
| 5898 | profile->clearAudioProfiles(); |
| 5899 | if (!profile->hasDynamicAudioProfile()) { |
| 5900 | continue; |
| 5901 | } |
| 5902 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 5903 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 5904 | // the capabilities of the devices that is supported by the IOProfile. |
| 5905 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 5906 | if (supportedDevice == device || |
| 5907 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 5908 | continue; |
| 5909 | } |
| 5910 | struct audio_port_v7 port; |
| 5911 | supportedDevice->toAudioPort(&port); |
| 5912 | status_t status = mpClientInterface->getAudioPort(&port); |
| 5913 | if (status == NO_ERROR) { |
| 5914 | supportedDevice->importAudioPort(port); |
| 5915 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5916 | } |
| 5917 | } |
| 5918 | } |
| 5919 | } |
| 5920 | return NO_ERROR; |
| 5921 | } |
| 5922 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5923 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5924 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5925 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5926 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5927 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5928 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5929 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5930 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5931 | } |
| 5932 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5933 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5934 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5935 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5936 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5937 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5938 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5939 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5940 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5941 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5942 | if (profile->supportsDevice(device)) { |
| 5943 | profiles.add(profile); |
| 5944 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 5945 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5946 | } |
| 5947 | } |
| 5948 | } |
| 5949 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5950 | if (profiles.isEmpty()) { |
| 5951 | ALOGW("%s: No input profile available for device %s", |
| 5952 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5953 | return BAD_VALUE; |
| 5954 | } |
| 5955 | |
| 5956 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 5957 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 5958 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 5959 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5960 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5961 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5962 | // nothing to do if one input is already opened for this profile |
| 5963 | size_t input_index; |
| 5964 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 5965 | desc = mInputs.valueAt(input_index); |
| 5966 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5967 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5968 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5969 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5970 | break; |
| 5971 | } |
| 5972 | } |
| 5973 | if (input_index != mInputs.size()) { |
| 5974 | continue; |
| 5975 | } |
| 5976 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5977 | if (!profile->canOpenNewIo()) { |
| 5978 | ALOGW("Max Input number %u already opened for this profile %s", |
| 5979 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 5980 | continue; |
| 5981 | } |
| 5982 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5983 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5984 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5985 | status_t status = desc->open(nullptr, |
| 5986 | device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5987 | AUDIO_SOURCE_MIC, |
| 5988 | AUDIO_INPUT_FLAG_NONE, |
| 5989 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5990 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5991 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5992 | const String8& address = String8(device->address().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5993 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5994 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5995 | mpClientInterface->setParameters(input, String8(param)); |
| 5996 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5997 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5998 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5999 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6000 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6001 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6002 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6003 | } |
| 6004 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 6005 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6006 | addInput(input, desc); |
| 6007 | } |
| 6008 | } // endif input != 0 |
| 6009 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 6010 | if (input == AUDIO_IO_HANDLE_NONE) { |
Patty | dd80758 | 2021-11-04 21:01:03 +0800 | [diff] [blame] | 6011 | ALOGW("%s could not open input for device %s", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6012 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6013 | profiles.removeAt(profile_index); |
| 6014 | profile_index--; |
| 6015 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6016 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 6017 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 6018 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6019 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 6020 | } |
| 6021 | } // end scan profiles |
| 6022 | |
| 6023 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6024 | 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] | 6025 | return BAD_VALUE; |
| 6026 | } |
| 6027 | } else { |
| 6028 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6029 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6030 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6031 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6032 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6033 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6034 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6035 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 6036 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 6037 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6038 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6039 | } |
| 6040 | } |
| 6041 | } |
| 6042 | } // end disconnect |
| 6043 | |
| 6044 | return NO_ERROR; |
| 6045 | } |
| 6046 | |
| 6047 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6048 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6049 | { |
| 6050 | ALOGV("closeOutput(%d)", output); |
| 6051 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6052 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 6053 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6054 | ALOGW("closeOutput() unknown output %d", output); |
| 6055 | return; |
| 6056 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6057 | const bool closingOutputWasActive = closingOutput->isActive(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6058 | mPolicyMixes.closeOutput(closingOutput); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6059 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6060 | // look for duplicated outputs connected to the output being removed. |
| 6061 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6062 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 6063 | if (dupOutput->isDuplicated() && |
| 6064 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 6065 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 6066 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6067 | // As all active tracks on duplicated output will be deleted, |
| 6068 | // and as they were also referenced on the other output, the reference |
| 6069 | // count for their stream type must be adjusted accordingly on |
| 6070 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6071 | const bool wasActive = remainingOutput->isActive(); |
| 6072 | // Note: no-op on the closing output where all clients has already been set inactive |
| 6073 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6074 | // stop() will be a no op if the output is still active but is needed in case all |
| 6075 | // active streams refcounts where cleared above |
| 6076 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6077 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 6078 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6079 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 6080 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 6081 | |
| 6082 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6083 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6084 | } |
| 6085 | } |
| 6086 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6087 | nextAudioPortGeneration(); |
| 6088 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6089 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6090 | if (index >= 0) { |
| 6091 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6092 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6093 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6094 | mAudioPatches.removeItemsAt(index); |
| 6095 | mpClientInterface->onAudioPatchListUpdate(); |
| 6096 | } |
| 6097 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 6098 | if (closingOutputWasActive) { |
| 6099 | closingOutput->stop(); |
| 6100 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 6101 | closingOutput->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6102 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6103 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6104 | mPreviousOutputs = mOutputs; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 6105 | if (closingOutput == mSpatializerOutput) { |
| 6106 | mSpatializerOutput.clear(); |
| 6107 | } |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6108 | |
| 6109 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 6110 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6111 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6112 | bool directOutputOpen = false; |
| 6113 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6114 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 6115 | directOutputOpen = true; |
| 6116 | break; |
| 6117 | } |
| 6118 | } |
| 6119 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6120 | ALOGV("no direct outputs open, reset MSD patches"); |
| 6121 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 6122 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 6123 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 6124 | // devices to patch to. This may be complicated by the fact that devices may become |
| 6125 | // unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6126 | setMsdOutputPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 6127 | } |
| 6128 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6129 | } |
| 6130 | |
| 6131 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 6132 | { |
| 6133 | ALOGV("closeInput(%d)", input); |
| 6134 | |
| 6135 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 6136 | if (inputDesc == NULL) { |
| 6137 | ALOGW("closeInput() unknown input %d", input); |
| 6138 | return; |
| 6139 | } |
| 6140 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6141 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6142 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6143 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6144 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6145 | if (index >= 0) { |
| 6146 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6147 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 6148 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6149 | mAudioPatches.removeItemsAt(index); |
| 6150 | mpClientInterface->onAudioPatchListUpdate(); |
| 6151 | } |
| 6152 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 6153 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 6154 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6155 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6156 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 6157 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6158 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 6159 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 6160 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6163 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 6164 | const DeviceVector &devices, |
| 6165 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6166 | { |
| 6167 | SortedVector<audio_io_handle_t> outputs; |
| 6168 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6169 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6170 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6171 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 6172 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6173 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6174 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6175 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6176 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6177 | outputs.add(openOutputs.keyAt(i)); |
| 6178 | } |
| 6179 | } |
| 6180 | return outputs; |
| 6181 | } |
| 6182 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6183 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 6184 | { |
| 6185 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 6186 | // output is suspended before any tracks are moved to it |
| 6187 | checkA2dpSuspend(); |
| 6188 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6189 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6190 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6191 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 6192 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 6193 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 6194 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 6195 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 6196 | // unnecessary rerouting by caching and reusing the arguments to |
| 6197 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 6198 | // This may be complicated by the fact that devices may become unavailable. |
Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 6199 | setMsdOutputPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 6200 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 6201 | // an event that changed routing likely occurred, inform upper layers |
| 6202 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 6203 | } |
| 6204 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6205 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 6206 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6207 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6208 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 6209 | mEngine->getProductStrategyForAttributes(rAttr); |
| 6210 | } |
| 6211 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6212 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 6213 | { |
| 6214 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6215 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6216 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6217 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6218 | && !isCallRxAudioSource(sourceDesc) && !sourceDesc->isInternal()) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6219 | connectAudioSource(sourceDesc); |
| 6220 | } |
| 6221 | } |
| 6222 | } |
| 6223 | |
| 6224 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 6225 | { |
| 6226 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 6227 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 6228 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 6229 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 6230 | disconnectAudioSource(sourceDesc); |
| 6231 | } |
| 6232 | } |
| 6233 | } |
| 6234 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6235 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 6236 | { |
| 6237 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 6238 | |
| 6239 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 6240 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 6241 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6242 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 6243 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6244 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6245 | uint32_t maxLatency = 0; |
| 6246 | bool invalidate = false; |
| 6247 | // take into account dynamic audio policies related changes: if a client is now associated |
| 6248 | // to a different policy mix than at creation time, invalidate corresponding stream |
| 6249 | for (size_t i = 0; i < mPreviousOutputs.size() && !invalidate; i++) { |
| 6250 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 6251 | if (desc->isDuplicated()) { |
| 6252 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6253 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6254 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 6255 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 6256 | continue; |
| 6257 | } |
| 6258 | sp<AudioPolicyMix> primaryMix; |
| 6259 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->uid(), |
| 6260 | client->flags(), primaryMix, nullptr); |
| 6261 | if (status != OK) { |
| 6262 | continue; |
| 6263 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 6264 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6265 | invalidate = true; |
| 6266 | if (desc->isStrategyActive(psId)) { |
| 6267 | maxLatency = desc->latency(); |
| 6268 | } |
| 6269 | break; |
| 6270 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 6271 | } |
| 6272 | } |
| 6273 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 6274 | if (srcOutputs != dstOutputs || invalidate) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6275 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 6276 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6277 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6278 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6279 | if (desc == nullptr) continue; |
| 6280 | |
| 6281 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6282 | maxLatency = desc->latency(); |
| 6283 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6284 | |
| 6285 | if (invalidate) continue; |
| 6286 | |
| 6287 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 6288 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6289 | // a client on a non direct outputs has necessarily a linear PCM format |
| 6290 | // so we can call selectOutput() safely |
| 6291 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 6292 | client->flags(), |
| 6293 | client->config().format, |
| 6294 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 6295 | client->config().sample_rate, |
| 6296 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6297 | if (newOutput != srcOut) { |
| 6298 | invalidate = true; |
| 6299 | break; |
| 6300 | } |
| 6301 | } else { |
| 6302 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 6303 | client->config().sample_rate, |
| 6304 | client->config().format, |
| 6305 | client->config().channel_mask, |
| 6306 | client->flags(), |
| 6307 | true /* directOnly */); |
| 6308 | if (profile != desc->mProfile) { |
| 6309 | invalidate = true; |
| 6310 | break; |
| 6311 | } |
| 6312 | } |
| 6313 | } |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6314 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6315 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 6316 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6317 | "%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] | 6318 | std::to_string(srcOutputs[0]).c_str(), |
| 6319 | std::to_string(dstOutputs[0]).c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6320 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6321 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 6322 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 6323 | if (desc == nullptr) continue; |
| 6324 | |
| 6325 | if (desc->isStrategyActive(psId)) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6326 | setStrategyMute(psId, true, desc); |
| 6327 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6328 | newDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6329 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6330 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6331 | if (source != nullptr && !isCallRxAudioSource(source) && !source->isInternal()) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6332 | connectAudioSource(source); |
| 6333 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6334 | } |
| 6335 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6336 | // Move effects associated to this stream from previous output to new output |
| 6337 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 6338 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6339 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6340 | // 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] | 6341 | if (invalidate) { |
| 6342 | for (auto stream : mEngine->getStreamTypesForProductStrategy(psId)) { |
| 6343 | mpClientInterface->invalidateStream(stream); |
| 6344 | } |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 6345 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 6346 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 6347 | if (desc == nullptr) continue; |
| 6348 | |
| 6349 | desc->setTracksInvalidatedStatusByStrategy(psId); |
| 6350 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6351 | } |
| 6352 | } |
| 6353 | } |
| 6354 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6355 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6356 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6357 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 6358 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 6359 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 6360 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6361 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6362 | } |
| 6363 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6364 | void AudioPolicyManager::checkSecondaryOutputs() { |
| 6365 | std::set<audio_stream_type_t> streamsToInvalidate; |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6366 | TrackSecondaryOutputsMap trackSecondaryOutputs; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6367 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 6368 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 6369 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6370 | sp<AudioPolicyMix> primaryMix; |
| 6371 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 6372 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->uid(), |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6373 | client->flags(), primaryMix, &secondaryMixes); |
| 6374 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 6375 | for (auto &secondaryMix : secondaryMixes) { |
| 6376 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 6377 | if (outputDesc != nullptr && |
| 6378 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 6379 | secondaryDescs.push_back(outputDesc); |
| 6380 | } |
| 6381 | } |
| 6382 | |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6383 | if (status != OK) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6384 | streamsToInvalidate.insert(client->stream()); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6385 | } else if (!std::equal( |
| 6386 | client->getSecondaryOutputs().begin(), |
| 6387 | client->getSecondaryOutputs().end(), |
| 6388 | secondaryDescs.begin(), secondaryDescs.end())) { |
jiabin | a528106 | 2021-11-23 00:10:23 +0000 | [diff] [blame] | 6389 | if (!audio_is_linear_pcm(client->config().format)) { |
| 6390 | // If the format is not PCM, the tracks should be invalidated to get correct |
| 6391 | // behavior when the secondary output is changed. |
| 6392 | streamsToInvalidate.insert(client->stream()); |
| 6393 | } else { |
| 6394 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; |
| 6395 | std::vector<audio_io_handle_t> secondaryOutputIds; |
| 6396 | for (const auto &secondaryDesc: secondaryDescs) { |
| 6397 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); |
| 6398 | weakSecondaryDescs.push_back(secondaryDesc); |
| 6399 | } |
| 6400 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); |
| 6401 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6402 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6403 | } |
| 6404 | } |
| 6405 | } |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6406 | if (!trackSecondaryOutputs.empty()) { |
| 6407 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); |
| 6408 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 6409 | for (audio_stream_type_t stream : streamsToInvalidate) { |
jiabin | 10a03f1 | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 6410 | 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] | 6411 | mpClientInterface->invalidateStream(stream); |
| 6412 | } |
| 6413 | } |
| 6414 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6415 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 6416 | AudioDeviceTypeAddrVector devices; |
| 6417 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 6418 | for (const auto &device : devices) { |
| 6419 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 6420 | return true; |
| 6421 | } |
| 6422 | } |
| 6423 | return false; |
| 6424 | } |
| 6425 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6426 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6427 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6428 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 6429 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6430 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6431 | return; |
| 6432 | } |
| 6433 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 6434 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6435 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 6436 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6437 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6438 | |
| 6439 | // if suspended, restore A2DP output if: |
| 6440 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6441 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6442 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6443 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6444 | // if not suspended, suspend A2DP output if: |
| 6445 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6446 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6447 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6448 | // |
| 6449 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6450 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6451 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6452 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6453 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6454 | |
| 6455 | mpClientInterface->restoreOutput(a2dpOutput); |
| 6456 | mA2dpSuspended = false; |
| 6457 | } |
| 6458 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6459 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6460 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 6461 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6462 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6463 | |
| 6464 | mpClientInterface->suspendOutput(a2dpOutput); |
| 6465 | mA2dpSuspended = true; |
| 6466 | } |
| 6467 | } |
| 6468 | } |
| 6469 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6470 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 6471 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6472 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6473 | DeviceVector devices; |
| 6474 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 6475 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6476 | if (index >= 0) { |
| 6477 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6478 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6479 | ALOGV("%s device %s forced by patch %d", __func__, |
| 6480 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 6481 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6482 | } |
| 6483 | } |
| 6484 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 6485 | // Do not retrieve engine device for outputs through MSD |
| 6486 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 6487 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 6488 | return outputDesc->devices(); |
| 6489 | } |
| 6490 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6491 | // Honor explicit routing requests only if no client using default routing is active on this |
| 6492 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 6493 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6494 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6495 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6496 | if (device != nullptr) { |
| 6497 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 6498 | } |
| 6499 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 6500 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 6501 | // of setForceUse / Default Bus device here |
| 6502 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 6503 | if (device != nullptr) { |
| 6504 | return DeviceVector(device); |
| 6505 | } |
| 6506 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6507 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 6508 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
| 6509 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6510 | auto hasStreamActive = [&](auto stream) { |
| 6511 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 6512 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 6513 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6514 | auto doGetOutputDevicesForVoice = [&]() { |
| 6515 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 6516 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL, false))) && |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6517 | (isInCall() || |
Henrik Backlund | 07c654a | 2021-10-14 15:57:10 +0200 | [diff] [blame] | 6518 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)) && |
| 6519 | !isStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE, 0); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 6520 | }; |
| 6521 | |
| 6522 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 6523 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 6524 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 6525 | // 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] | 6526 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 6527 | // stream is associated to the output descriptor. |
| 6528 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 6529 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 6530 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 6531 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6532 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 6533 | // check the route (would force modifying configuration file for this profile) |
| 6534 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 6535 | break; |
| 6536 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6537 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6538 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6539 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6540 | } |
| 6541 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6542 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 6543 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6544 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6545 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6546 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6547 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6548 | if (index >= 0) { |
| 6549 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6550 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6551 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 6552 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 6553 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6554 | } |
| 6555 | } |
| 6556 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6557 | // Honor explicit routing requests only if no client using default routing is active on this |
| 6558 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 6559 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6560 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 6561 | if (device != nullptr) { |
| 6562 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 6563 | } |
| 6564 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 6565 | // 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] | 6566 | // a null sp<>, causing the patch on the input stream to be released. |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6567 | audio_attributes_t attributes; |
| 6568 | uid_t uid; |
| 6569 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); |
| 6570 | if (topClient != nullptr) { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 6571 | attributes = topClient->attributes(); |
| 6572 | uid = topClient->uid(); |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6573 | } else { |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 6574 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; |
| 6575 | uid = 0; |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6576 | } |
| 6577 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6578 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 6579 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 6580 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6581 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 6582 | device = mEngine->getInputDeviceForAttributes(attributes, uid); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 6583 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6584 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6585 | return device; |
| 6586 | } |
| 6587 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 6588 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 6589 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 6590 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 6591 | } |
| 6592 | |
Dorin Drimus | f2196d8 | 2022-01-03 12:11:18 +0100 | [diff] [blame] | 6593 | // TODO - consider MSD routes b/214971780 |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6594 | status_t AudioPolicyManager::getDevicesForAttributes( |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6595 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices, bool forVolume) { |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6596 | if (devices == nullptr) { |
| 6597 | return BAD_VALUE; |
| 6598 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6599 | |
| 6600 | // Devices are determined in the following precedence: |
| 6601 | // |
| 6602 | // 1) Devices associated with a dynamic policy matching the attributes. This is often |
| 6603 | // a remote submix from MIX_ROUTE_FLAG_LOOP_BACK. |
| 6604 | // |
| 6605 | // If no such dynamic policy then |
| 6606 | // 2) Devices containing an active client using setPreferredDevice |
| 6607 | // with same strategy as the attributes. |
| 6608 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 6609 | // |
| 6610 | // If no corresponding active client with setPreferredDevice then |
| 6611 | // 3) Devices associated with the strategy determined by the attributes |
| 6612 | // (from the default Engine::getOutputDevicesForAttributes() implementation). |
| 6613 | // |
| 6614 | // See related getOutputForAttrInt(). |
| 6615 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6616 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 6617 | // audio routing, only used for duplication for playback capture) |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 6618 | sp<AudioPolicyMix> policyMix; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6619 | status_t status = mPolicyMixes.getOutputForAttr(attr, 0 /*uid unknown here*/, |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6620 | AUDIO_OUTPUT_FLAG_NONE, policyMix, nullptr /* secondaryMixes */); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6621 | if (status != OK) { |
| 6622 | return status; |
| 6623 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6624 | |
| 6625 | DeviceVector curDevices; |
| 6626 | if (policyMix != nullptr && policyMix->getOutput() != nullptr && |
| 6627 | // For volume control, skip LOOPBACK mixes which use AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
| 6628 | // as they are unaffected by device/stream volume |
| 6629 | // (per SwAudioOutputDescriptor::isFixedVolume()). |
| 6630 | (!forVolume || policyMix->mDeviceType != AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
| 6631 | ) { |
| 6632 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 6633 | policyMix->mDeviceType, policyMix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
| 6634 | curDevices.add(deviceDesc); |
| 6635 | } else { |
| 6636 | // The default Engine::getOutputDevicesForAttributes() uses findPreferredDevice() |
| 6637 | // which selects setPreferredDevice if active. This means forVolume call |
| 6638 | // will take an active setPreferredDevice, if such exists. |
| 6639 | |
| 6640 | curDevices = mEngine->getOutputDevicesForAttributes( |
| 6641 | attr, nullptr /* preferredDevice */, false /* fromCache */); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6642 | } |
Andy Hung | 6d23c0f | 2022-02-16 09:37:15 -0800 | [diff] [blame] | 6643 | |
| 6644 | if (forVolume) { |
| 6645 | // We alias the device AUDIO_DEVICE_OUT_SPEAKER_SAFE to AUDIO_DEVICE_OUT_SPEAKER |
| 6646 | // for single volume control in AudioService (such relationship should exist if |
| 6647 | // SPEAKER_SAFE is present). |
| 6648 | // |
| 6649 | // (This is unrelated to a different device grouping as Volume::getDeviceCategory) |
| 6650 | DeviceVector speakerSafeDevices = |
| 6651 | curDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 6652 | if (!speakerSafeDevices.isEmpty()) { |
| 6653 | curDevices.merge( |
| 6654 | mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
| 6655 | curDevices.remove(speakerSafeDevices); |
| 6656 | } |
| 6657 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 6658 | for (const auto& device : curDevices) { |
| 6659 | devices->push_back(device->getDeviceTypeAddr()); |
| 6660 | } |
| 6661 | return NO_ERROR; |
| 6662 | } |
| 6663 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6664 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6665 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6666 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6667 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6668 | updateDevicesAndOutputs(); |
| 6669 | break; |
| 6670 | default: |
| 6671 | break; |
| 6672 | } |
| 6673 | } |
| 6674 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6675 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 6676 | |
| 6677 | // skip beacon mute management if a dedicated TTS output is available |
| 6678 | if (mTtsOutputAvailable) { |
| 6679 | return 0; |
| 6680 | } |
| 6681 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6682 | switch(event) { |
| 6683 | case STARTING_OUTPUT: |
| 6684 | mBeaconMuteRefCount++; |
| 6685 | break; |
| 6686 | case STOPPING_OUTPUT: |
| 6687 | if (mBeaconMuteRefCount > 0) { |
| 6688 | mBeaconMuteRefCount--; |
| 6689 | } |
| 6690 | break; |
| 6691 | case STARTING_BEACON: |
| 6692 | mBeaconPlayingRefCount++; |
| 6693 | break; |
| 6694 | case STOPPING_BEACON: |
| 6695 | if (mBeaconPlayingRefCount > 0) { |
| 6696 | mBeaconPlayingRefCount--; |
| 6697 | } |
| 6698 | break; |
| 6699 | } |
| 6700 | |
| 6701 | if (mBeaconMuteRefCount > 0) { |
| 6702 | // any playback causes beacon to be muted |
| 6703 | return setBeaconMute(true); |
| 6704 | } else { |
| 6705 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 6706 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 6711 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 6712 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 6713 | // keep track of muted state to avoid repeating mute/unmute operations |
| 6714 | if (mBeaconMuted != mute) { |
| 6715 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 6716 | ALOGV("\t muting %d", mute); |
| 6717 | uint32_t maxLatency = 0; |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 6718 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS, false); |
| 6719 | if (ttsVolumeSource == VOLUME_SOURCE_NONE) { |
| 6720 | ALOGV("\t no tts volume source available"); |
| 6721 | return 0; |
| 6722 | } |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6723 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6724 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6725 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6726 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 6727 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6728 | maxLatency = latency; |
| 6729 | } |
| 6730 | } |
| 6731 | mBeaconMuted = mute; |
| 6732 | return maxLatency; |
| 6733 | } |
| 6734 | return 0; |
| 6735 | } |
| 6736 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6737 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6738 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6739 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6740 | mPreviousOutputs = mOutputs; |
| 6741 | } |
| 6742 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 6743 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6744 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6745 | uint32_t delayMs) |
| 6746 | { |
| 6747 | // mute/unmute strategies using an incompatible device combination |
| 6748 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 6749 | // if unmuting, unmute only after the specified delay |
| 6750 | if (outputDesc->isDuplicated()) { |
| 6751 | return 0; |
| 6752 | } |
| 6753 | |
| 6754 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6755 | DeviceVector devices = outputDesc->devices(); |
| 6756 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6757 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6758 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 6759 | for (const auto &productStrategy : productStrategies) { |
| 6760 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 6761 | DeviceVector curDevices = |
| 6762 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 6763 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 6764 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6765 | bool doMute = false; |
| 6766 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6767 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6768 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6769 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 6770 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6771 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6772 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6773 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6774 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6775 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6776 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6777 | // 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] | 6778 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6779 | continue; |
| 6780 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6781 | ALOGVV("%s() %s (curDevice %s)", __func__, |
| 6782 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 6783 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 6784 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6785 | if (mute) { |
| 6786 | // FIXME: should not need to double latency if volume could be applied |
| 6787 | // immediately by the audioflinger mixer. We must account for the delay |
| 6788 | // between now and the next time the audioflinger thread for this output |
| 6789 | // will process a buffer (which corresponds to one buffer size, |
| 6790 | // usually 1/2 or 1/4 of the latency). |
| 6791 | if (muteWaitMs < desc->latency() * 2) { |
| 6792 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6793 | } |
| 6794 | } |
| 6795 | } |
| 6796 | } |
| 6797 | } |
| 6798 | } |
| 6799 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6800 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 6801 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6802 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 6803 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 6804 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 6805 | if (muteWaitMs < tempMuteWaitMs) { |
| 6806 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6807 | } |
Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 6808 | |
| 6809 | // If recommended duration is defined, replace temporary mute duration to avoid |
| 6810 | // truncated notifications at beginning, which depends on duration of changing path in HAL. |
| 6811 | // Otherwise, temporary mute duration is conservatively set to 4 times the reported latency. |
| 6812 | uint32_t tempRecommendedMuteDuration = outputDesc->getRecommendedMuteDurationMs(); |
| 6813 | uint32_t tempMuteDurationMs = tempRecommendedMuteDuration > 0 ? |
| 6814 | tempRecommendedMuteDuration : outputDesc->latency() * 4; |
| 6815 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6816 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 6817 | // make sure that we do not start the temporary mute period too early in case of |
| 6818 | // delayed device change |
| 6819 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 6820 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6821 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6822 | } |
| 6823 | } |
| 6824 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6825 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 6826 | if (muteWaitMs > delayMs) { |
| 6827 | muteWaitMs -= delayMs; |
| 6828 | usleep(muteWaitMs * 1000); |
| 6829 | return muteWaitMs; |
| 6830 | } |
| 6831 | return 0; |
| 6832 | } |
| 6833 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6834 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 6835 | const DeviceVector &devices, |
| 6836 | bool force, |
| 6837 | int delayMs, |
| 6838 | audio_patch_handle_t *patchHandle, |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6839 | bool requiresMuteCheck, bool requiresVolumeCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6840 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6841 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6842 | uint32_t muteWaitMs; |
| 6843 | |
| 6844 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6845 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 6846 | nullptr /* patchHandle */, requiresMuteCheck); |
| 6847 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 6848 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6849 | return muteWaitMs; |
| 6850 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6851 | |
| 6852 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6853 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 6854 | DeviceVector prevDevices = outputDesc->devices(); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6855 | DeviceVector availPrevDevices = mAvailableOutputDevices.filter(prevDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6856 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6857 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 6858 | |
| 6859 | if (!filteredDevices.isEmpty()) { |
| 6860 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6861 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6862 | |
| 6863 | // if the outputs are not materially active, there is no need to mute. |
| 6864 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6865 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6866 | } else { |
| 6867 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 6868 | muteWaitMs = 0; |
| 6869 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6870 | |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6871 | bool outputRouted = outputDesc->isRouted(); |
| 6872 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 6873 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 6874 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 6875 | // available (otherwise reset device must be done on the output) |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6876 | if (!devices.isEmpty() && filteredDevices.isEmpty() && !availPrevDevices.empty()) { |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 6877 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 6878 | // restore previous device after evaluating strategy mute state |
| 6879 | outputDesc->setDevices(prevDevices); |
| 6880 | return muteWaitMs; |
| 6881 | } |
| 6882 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6883 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 6884 | // the requested device is AUDIO_DEVICE_NONE |
| 6885 | // OR the requested device is the same as current device |
| 6886 | // AND force is not specified |
| 6887 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6888 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6889 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && !force && outputRouted) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6890 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 6891 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6892 | if (requiresVolumeCheck && !filteredDevices.isEmpty()) { |
| 6893 | ALOGV("%s setting same device on routed output, force apply volumes", __func__); |
| 6894 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs, true /*force*/); |
| 6895 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6896 | return muteWaitMs; |
| 6897 | } |
| 6898 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6899 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6900 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6901 | // do the routing |
Francois Gaffie | 3523ab3 | 2021-06-22 13:24:34 +0200 | [diff] [blame] | 6902 | if (filteredDevices.isEmpty() || mAvailableOutputDevices.filter(filteredDevices).empty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6903 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6904 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6905 | PatchBuilder patchBuilder; |
| 6906 | patchBuilder.addSource(outputDesc); |
| 6907 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 6908 | for (const auto &filteredDevice : filteredDevices) { |
| 6909 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 6910 | } |
| 6911 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 6912 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 6913 | // to avoid disordered sequence of muting volume and changing devices. |
| 6914 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), |
| 6915 | muteWaitMs == 0 ? (delayMs + (outputDesc->latency() / 2)) : delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6916 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6917 | |
| 6918 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6919 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6920 | |
| 6921 | return muteWaitMs; |
| 6922 | } |
| 6923 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6924 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6925 | int delayMs, |
| 6926 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6927 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6928 | ssize_t index; |
Francois Gaffie | b2e5cb5 | 2021-06-22 13:16:09 +0200 | [diff] [blame] | 6929 | if (patchHandle == nullptr && !outputDesc->isRouted()) { |
| 6930 | return INVALID_OPERATION; |
| 6931 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6932 | if (patchHandle) { |
| 6933 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 6934 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 6935 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6936 | } |
| 6937 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6938 | return INVALID_OPERATION; |
| 6939 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6940 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6941 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6942 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 6943 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6944 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6945 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 6946 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6947 | return status; |
| 6948 | } |
| 6949 | |
| 6950 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6951 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6952 | bool force, |
| 6953 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6954 | { |
| 6955 | status_t status = NO_ERROR; |
| 6956 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6957 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6958 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 6959 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6960 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6961 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6962 | PatchBuilder patchBuilder; |
| 6963 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 6964 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 6965 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6966 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 6967 | auto result = usecase; |
| 6968 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 6969 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 6970 | } |
| 6971 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6972 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6973 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6974 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6975 | } |
| 6976 | } |
| 6977 | return status; |
| 6978 | } |
| 6979 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6980 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 6981 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6982 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6983 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6984 | ssize_t index; |
| 6985 | if (patchHandle) { |
| 6986 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 6987 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6988 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6989 | } |
| 6990 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6991 | return INVALID_OPERATION; |
| 6992 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6993 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6994 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6995 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 6996 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6997 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6998 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 6999 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 7000 | return status; |
| 7001 | } |
| 7002 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7003 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7004 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7005 | audio_format_t& format, |
| 7006 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7007 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7008 | { |
| 7009 | // Choose an input profile based on the requested capture parameters: select the first available |
| 7010 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7011 | // |
| 7012 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 7013 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7014 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7015 | sp<IOProfile> firstInexact; |
| 7016 | uint32_t updatedSamplingRate = 0; |
| 7017 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 7018 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 7019 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 7020 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 7021 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7022 | //updatedFormat = format; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7023 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7024 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7025 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7026 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 7027 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7028 | &channelMask /*updatedChannelMask*/, |
| 7029 | // FIXME ugly cast |
| 7030 | (audio_output_flags_t) flags, |
| 7031 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7032 | return profile; |
| 7033 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 7034 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7035 | samplingRate, |
| 7036 | &updatedSamplingRate, |
| 7037 | format, |
| 7038 | &updatedFormat, |
| 7039 | channelMask, |
| 7040 | &updatedChannelMask, |
| 7041 | // FIXME ugly cast |
| 7042 | (audio_output_flags_t) flags, |
| 7043 | false /*exactMatchRequiredForInputFlags*/)) { |
| 7044 | firstInexact = profile; |
| 7045 | } |
| 7046 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7047 | } |
| 7048 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 7049 | if (firstInexact != nullptr) { |
| 7050 | samplingRate = updatedSamplingRate; |
| 7051 | format = updatedFormat; |
| 7052 | channelMask = updatedChannelMask; |
| 7053 | return firstInexact; |
| 7054 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7055 | return NULL; |
| 7056 | } |
| 7057 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7058 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 7059 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 7060 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7061 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7062 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7063 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7064 | |
| 7065 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 7066 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 7067 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 7068 | // the ringtone volume |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7069 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7070 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING, false); |
| 7071 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC, false); |
| 7072 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM, false); |
| 7073 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY, false); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7074 | |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7075 | if (volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7076 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 7077 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 7078 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7079 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7080 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 7081 | } |
| 7082 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 7083 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7084 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 7085 | mOutputs.isActiveLocally(callVolumeSrc))) && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7086 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7087 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 7088 | volumeSource == alarmVolumeSrc || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7089 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false) || |
| 7090 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
| 7091 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF, false) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7092 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7093 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7094 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7095 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7096 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 7097 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7098 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 7099 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 7100 | // programmatically muted. |
| 7101 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 7102 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 7103 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 7104 | bool exemptFromCapping = |
| 7105 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 7106 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 7107 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 7108 | volumeSource, volumeDb); |
| 7109 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7110 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 7111 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 7112 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 7113 | } |
| 7114 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7115 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 7116 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 7117 | // - always attenuate notifications volume by 6dB |
| 7118 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 7119 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7120 | // - if music is playing, always limit the volume to current music volume, |
| 7121 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7122 | if (!Intersection(deviceTypes, |
| 7123 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 7124 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 7125 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 7126 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7127 | ((volumeSource == alarmVolumeSrc || |
| 7128 | volumeSource == ringVolumeSrc) || |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7129 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION, false)) || |
| 7130 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM, false)) || |
| 7131 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false)) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7132 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 7133 | curves.canBeMuted()) { |
| 7134 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7135 | // when the phone is ringing we must consider that music could have been paused just before |
| 7136 | // by the music application and behave as if music was active if the last music track was |
| 7137 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 7138 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7139 | mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7140 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7141 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7142 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 7143 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7144 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7145 | float musicVolDb = computeVolume(musicCurves, |
| 7146 | musicVolumeSrc, |
| 7147 | musicCurves.getVolumeIndex(musicDevice), |
| 7148 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7149 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 7150 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 7151 | if (volumeDb > minVolDb) { |
| 7152 | volumeDb = minVolDb; |
| 7153 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7154 | } |
Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 7155 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER |
| 7156 | && !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 7157 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7158 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 7159 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7160 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7161 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7162 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 7163 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7164 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 7165 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 7166 | } |
| 7167 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7168 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7169 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7170 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7171 | } |
| 7172 | } |
| 7173 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 7174 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7175 | } |
| 7176 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7177 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7178 | VolumeSource fromVolumeSource, |
| 7179 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7180 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7181 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7182 | return srcIndex; |
| 7183 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 7184 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 7185 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7186 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 7187 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 7188 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 7189 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7190 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 7191 | // preserve mute request or correct range |
| 7192 | if (srcIndex < minSrc) { |
| 7193 | if (srcIndex == 0) { |
| 7194 | return 0; |
| 7195 | } |
| 7196 | srcIndex = minSrc; |
| 7197 | } else if (srcIndex > maxSrc) { |
| 7198 | srcIndex = maxSrc; |
| 7199 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 7200 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 7201 | } |
| 7202 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7203 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 7204 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7205 | int index, |
| 7206 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7207 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7208 | int delayMs, |
| 7209 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7210 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7211 | // do not change actual attributes volume if the attributes is muted |
| 7212 | if (outputDesc->isMuted(volumeSource)) { |
| 7213 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 7214 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7215 | return NO_ERROR; |
| 7216 | } |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7217 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL, false); |
| 7218 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO, false); |
| 7219 | bool isVoiceVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (callVolSrc == volumeSource); |
| 7220 | bool isBtScoVolSrc = (volumeSource != VOLUME_SOURCE_NONE) && (btScoVolSrc == volumeSource); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7221 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7222 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7223 | // 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] | 7224 | // if sco and call follow same curves, bypass forceUseForComm |
| 7225 | if ((callVolSrc != btScoVolSrc) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 7226 | ((isVoiceVolSrc && isScoRequested) || |
| 7227 | (isBtScoVolSrc && !isScoRequested))) { |
| 7228 | ALOGV("%s cannot set volume group %d volume when is%srequested for comm", __func__, |
| 7229 | volumeSource, isScoRequested ? " " : "n ot "); |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 7230 | // Do not return an error here as AudioService will always set both voice call |
| 7231 | // and bluetooth SCO volumes due to stream aliasing. |
| 7232 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7233 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7234 | if (deviceTypes.empty()) { |
| 7235 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7236 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7237 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7238 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 7239 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 7240 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 7241 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7242 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 7243 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7244 | } |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7245 | const bool muted = (index == 0) && (volumeDb != 0.0f); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7246 | outputDesc->setVolume( |
Francois Gaffie | 593634d | 2021-06-22 13:31:31 +0200 | [diff] [blame] | 7247 | volumeDb, muted, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7248 | |
Eric Laurent | e8f2c0f | 2021-08-17 11:17:19 +0200 | [diff] [blame] | 7249 | if (outputDesc == mPrimaryOutput && (isVoiceVolSrc || isBtScoVolSrc)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7250 | float voiceVolume; |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7251 | // 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] | 7252 | if (isVoiceVolSrc) { |
| 7253 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7254 | } else { |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 7255 | voiceVolume = index == 0 ? 0.0 : 1.0; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7256 | } |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 7257 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7258 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 7259 | mLastVoiceVolume = voiceVolume; |
| 7260 | } |
| 7261 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7262 | return NO_ERROR; |
| 7263 | } |
| 7264 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7265 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7266 | const DeviceTypeSet& deviceTypes, |
| 7267 | int delayMs, |
| 7268 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7269 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 7270 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7271 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 7272 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 7273 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7274 | curves.getVolumeIndex(deviceTypes), |
| 7275 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7276 | } |
| 7277 | } |
| 7278 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7279 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 7280 | bool on, |
| 7281 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7282 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7283 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7284 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7285 | std::vector<VolumeSource> sourcesToMute; |
| 7286 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 7287 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 7288 | toString(attributes).c_str(), on, outputDesc->getId()); |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7289 | VolumeSource source = toVolumeSource(attributes, false); |
| 7290 | if ((source != VOLUME_SOURCE_NONE) && |
| 7291 | (std::find(begin(sourcesToMute), end(sourcesToMute), source) |
| 7292 | == end(sourcesToMute))) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7293 | sourcesToMute.push_back(source); |
| 7294 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7295 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7296 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7297 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7298 | } |
| 7299 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7300 | } |
| 7301 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7302 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 7303 | bool on, |
| 7304 | const sp<AudioOutputDescriptor>& outputDesc, |
| 7305 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7306 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7307 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7308 | if (deviceTypes.empty()) { |
| 7309 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7310 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7311 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7312 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7313 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 7314 | if (curves.canBeMuted() && |
Francois Gaffie | 4404ddb | 2021-02-04 17:03:38 +0100 | [diff] [blame] | 7315 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE, false) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7316 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 7317 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7318 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7319 | } |
| 7320 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7321 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 7322 | // ignored |
| 7323 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7324 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7325 | if (!outputDesc->isMuted(volumeSource)) { |
| 7326 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7327 | return; |
| 7328 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 7329 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 7330 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7331 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 7332 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 7333 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 7334 | delayMs); |
| 7335 | } |
| 7336 | } |
| 7337 | } |
| 7338 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 7339 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 7340 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 7341 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7342 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 7343 | return true; |
| 7344 | } |
| 7345 | |
| 7346 | // has known usage? |
| 7347 | switch (paa->usage) { |
| 7348 | case AUDIO_USAGE_UNKNOWN: |
| 7349 | case AUDIO_USAGE_MEDIA: |
| 7350 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 7351 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 7352 | case AUDIO_USAGE_ALARM: |
| 7353 | case AUDIO_USAGE_NOTIFICATION: |
| 7354 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 7355 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 7356 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 7357 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 7358 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 7359 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 7360 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 7361 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 7362 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 7363 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 7364 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 7365 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 7366 | case AUDIO_USAGE_EMERGENCY: |
| 7367 | case AUDIO_USAGE_SAFETY: |
| 7368 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 7369 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 7370 | break; |
| 7371 | default: |
| 7372 | return false; |
| 7373 | } |
| 7374 | return true; |
| 7375 | } |
| 7376 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 7377 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 7378 | { |
| 7379 | return mEngine->getForceUse(usage); |
| 7380 | } |
| 7381 | |
| 7382 | bool AudioPolicyManager::isInCall() |
| 7383 | { |
| 7384 | return isStateInCall(mEngine->getPhoneState()); |
| 7385 | } |
| 7386 | |
| 7387 | bool AudioPolicyManager::isStateInCall(int state) |
| 7388 | { |
| 7389 | return is_state_in_call(state); |
| 7390 | } |
| 7391 | |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7392 | bool AudioPolicyManager::isCallAudioAccessible() |
| 7393 | { |
| 7394 | audio_mode_t mode = mEngine->getPhoneState(); |
| 7395 | return (mode == AUDIO_MODE_IN_CALL) |
Eric Laurent | c8c4f1f | 2021-11-09 11:51:34 +0100 | [diff] [blame] | 7396 | || (mode == AUDIO_MODE_CALL_SCREEN) |
| 7397 | || (mode == AUDIO_MODE_CALL_REDIRECT); |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 7398 | } |
| 7399 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7400 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 7401 | { |
| 7402 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 7403 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7404 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 7405 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 7406 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7407 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7408 | } |
| 7409 | } |
| 7410 | |
| 7411 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 7412 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 7413 | bool release = false; |
| 7414 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 7415 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 7416 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 7417 | source->ext.device.type == deviceDesc->type()) { |
| 7418 | release = true; |
| 7419 | } |
| 7420 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7421 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7422 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 7423 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 7424 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 7425 | sink->ext.device.type == deviceDesc->type() && |
| 7426 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 7427 | || strncmp(sink->ext.device.address, address, |
| 7428 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7429 | release = true; |
| 7430 | } |
| 7431 | } |
| 7432 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7433 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 7434 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7435 | } |
| 7436 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7437 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 7438 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 7439 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 7440 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7441 | } |
| 7442 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7443 | void AudioPolicyManager::modifySurroundFormats( |
| 7444 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7445 | std::unordered_set<audio_format_t> enforcedSurround( |
| 7446 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7447 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 7448 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 7449 | allSurround.insert(pair.first); |
| 7450 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 7451 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7452 | |
| 7453 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7454 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7455 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7456 | // This is the resulting set of formats depending on the surround mode: |
| 7457 | // 'all surround' = allSurround |
| 7458 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 7459 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 7460 | // 'manual surround' = mManualSurroundFormats |
| 7461 | // AUTO: formats v 'enforced surround' |
| 7462 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 7463 | // NEVER: formats ^ 'non-surround' |
| 7464 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7465 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7466 | std::unordered_set<audio_format_t> formatSet; |
| 7467 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 7468 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7469 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7470 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7471 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7472 | formatSet.insert(*formatIter); |
| 7473 | } |
| 7474 | } |
| 7475 | } else { |
| 7476 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 7477 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7478 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7479 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7480 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7481 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7482 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 7483 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 7484 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7485 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7486 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 7487 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 7488 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 7489 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7490 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7491 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7492 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7493 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7494 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7495 | } |
| 7496 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7497 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 7498 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7499 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 7500 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 7501 | |
| 7502 | // If NEVER, then remove support for channelMasks > stereo. |
| 7503 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7504 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 7505 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7506 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 7507 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7508 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7509 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7510 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7511 | } |
| 7512 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7513 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 7514 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 7515 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7516 | bool supports5dot1 = false; |
| 7517 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 7518 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7519 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 7520 | supports5dot1 = true; |
| 7521 | break; |
| 7522 | } |
| 7523 | } |
| 7524 | // If not then add 5.1 support. |
| 7525 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7526 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 7527 | 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] | 7528 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7529 | } |
| 7530 | } |
| 7531 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7532 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 7533 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7534 | AudioProfileVector &profiles) |
| 7535 | { |
| 7536 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7537 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7538 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7539 | // Format MUST be checked first to update the list of AudioProfile |
| 7540 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7541 | reply = mpClientInterface->getParameters( |
| 7542 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 7543 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7544 | AudioParameter repliedParameters(reply); |
| 7545 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7546 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7547 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 7548 | return; |
| 7549 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 7550 | FormatVector formats = formatsFromString(reply.string()); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 7551 | mReportedFormatsMap[devDesc] = formats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7552 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 7553 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7554 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 7555 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 7556 | addProfilesForFormats(profiles, formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7557 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7558 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 7559 | for (audio_format_t format : profiles.getSupportedFormats()) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 7560 | ChannelMaskSet channelMasks; |
| 7561 | SampleRateSet samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7562 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7563 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7564 | |
| 7565 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7566 | reply = mpClientInterface->getParameters( |
| 7567 | ioHandle, |
| 7568 | requestedParameters.toString() + ";" + |
| 7569 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7570 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7571 | AudioParameter repliedParameters(reply); |
| 7572 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7573 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7574 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7575 | } |
| 7576 | } |
| 7577 | if (profiles.hasDynamicChannelsFor(format)) { |
| 7578 | reply = mpClientInterface->getParameters(ioHandle, |
| 7579 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7580 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7581 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7582 | AudioParameter repliedParameters(reply); |
| 7583 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 7584 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 7585 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 7586 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 7587 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 7588 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 7589 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7590 | } |
| 7591 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 7592 | addDynamicAudioProfileAndSort( |
| 7593 | profiles, new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 7594 | } |
| 7595 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 7596 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7597 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 7598 | audio_patch_handle_t *patchHandle, |
| 7599 | AudioIODescriptorInterface *ioDescriptor, |
| 7600 | const struct audio_patch *patch, |
| 7601 | int delayMs) |
| 7602 | { |
| 7603 | ssize_t index = mAudioPatches.indexOfKey( |
| 7604 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 7605 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 7606 | sp<AudioPatch> patchDesc; |
| 7607 | status_t status = installPatch( |
| 7608 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 7609 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7610 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7611 | } |
| 7612 | return status; |
| 7613 | } |
| 7614 | |
| 7615 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 7616 | ssize_t index, |
| 7617 | audio_patch_handle_t *patchHandle, |
| 7618 | const struct audio_patch *patch, |
| 7619 | int delayMs, |
| 7620 | uid_t uid, |
| 7621 | sp<AudioPatch> *patchDescPtr) |
| 7622 | { |
| 7623 | sp<AudioPatch> patchDesc; |
| 7624 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 7625 | if (index >= 0) { |
| 7626 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7627 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7628 | } |
| 7629 | |
| 7630 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 7631 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 7632 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 7633 | if (status == NO_ERROR) { |
| 7634 | if (index < 0) { |
| 7635 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7636 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7637 | } else { |
| 7638 | patchDesc->mPatch = *patch; |
| 7639 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7640 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7641 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 7642 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 7643 | } |
| 7644 | nextAudioPortGeneration(); |
| 7645 | mpClientInterface->onAudioPatchListUpdate(); |
| 7646 | } |
| 7647 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 7648 | return status; |
| 7649 | } |
| 7650 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7651 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 7652 | { |
| 7653 | const TrackClientVector activeClients = output->getActiveClients(); |
| 7654 | if (activeClients.empty()) { |
| 7655 | return true; |
| 7656 | } |
| 7657 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 7658 | if (index < 0) { |
| 7659 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 7660 | __func__, output->getId()); |
| 7661 | return false; |
| 7662 | } |
| 7663 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 7664 | DeviceVector routedDevices; |
| 7665 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 7666 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 7667 | patchDesc->mPatch.sinks[i].id); |
| 7668 | if (device == nullptr) { |
| 7669 | ALOGE("%s, no audio device found with id(%d)", |
| 7670 | __func__, patchDesc->mPatch.sinks[i].id); |
| 7671 | return false; |
| 7672 | } |
| 7673 | routedDevices.add(device); |
| 7674 | } |
| 7675 | for (const auto& client : activeClients) { |
jiabin | 4925685 | 2022-03-09 11:21:35 -0800 | [diff] [blame] | 7676 | if (client->isInvalid()) { |
| 7677 | // No need to take care about invalidated clients. |
| 7678 | continue; |
| 7679 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7680 | sp<DeviceDescriptor> preferredDevice = |
| 7681 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 7682 | if (mEngine->getOutputDevicesForAttributes( |
| 7683 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 7684 | return false; |
| 7685 | } |
| 7686 | } |
| 7687 | return true; |
| 7688 | } |
| 7689 | |
| 7690 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7691 | const sp<IOProfile>& profile, const DeviceVector& devices, |
| 7692 | const audio_config_base_t *mixerConfig) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7693 | { |
| 7694 | for (const auto& device : devices) { |
| 7695 | // TODO: This should be checking if the profile supports the device combo. |
| 7696 | if (!profile->supportsDevice(device)) { |
| 7697 | return nullptr; |
| 7698 | } |
| 7699 | } |
| 7700 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 7701 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7702 | status_t status = desc->open(nullptr /* halConfig */, mixerConfig, devices, |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7703 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
| 7704 | if (status != NO_ERROR) { |
| 7705 | return nullptr; |
| 7706 | } |
| 7707 | |
| 7708 | // Here is where the out_set_parameters() for card & device gets called |
| 7709 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 7710 | const audio_devices_t deviceType = device->type(); |
| 7711 | const String8 &address = String8(device->address().c_str()); |
| 7712 | if (!address.isEmpty()) { |
| 7713 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 7714 | mpClientInterface->setParameters(output, String8(param)); |
| 7715 | free(param); |
| 7716 | } |
| 7717 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
| 7718 | if (!profile->hasValidAudioProfile()) { |
| 7719 | ALOGW("%s() missing param", __func__); |
| 7720 | desc->close(); |
| 7721 | return nullptr; |
| 7722 | } else if (profile->hasDynamicAudioProfile()) { |
| 7723 | desc->close(); |
| 7724 | output = AUDIO_IO_HANDLE_NONE; |
| 7725 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 7726 | profile->pickAudioProfile( |
| 7727 | config.sample_rate, config.channel_mask, config.format); |
| 7728 | config.offload_info.sample_rate = config.sample_rate; |
| 7729 | config.offload_info.channel_mask = config.channel_mask; |
| 7730 | config.offload_info.format = config.format; |
| 7731 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7732 | status = desc->open(&config, mixerConfig, devices, |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7733 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
| 7734 | if (status != NO_ERROR) { |
| 7735 | return nullptr; |
| 7736 | } |
| 7737 | } |
| 7738 | |
| 7739 | addOutput(output, desc); |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7740 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7741 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 7742 | sp<AudioPolicyMix> policyMix; |
| 7743 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 7744 | policyMix->setOutput(desc); |
| 7745 | desc->mPolicyMix = policyMix; |
| 7746 | } else { |
| 7747 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
| 7748 | address.string()); |
| 7749 | } |
| 7750 | |
Eric Laurent | b4f42a9 | 2022-01-17 17:37:31 +0100 | [diff] [blame] | 7751 | } else if (hasPrimaryOutput() && profile->getModule() |
| 7752 | != mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY) |
| 7753 | && ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) { |
| 7754 | // no duplicated output for: |
| 7755 | // - direct outputs |
| 7756 | // - outputs used by dynamic policy mixes |
| 7757 | // - outputs opened on the primary HW module |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7758 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 7759 | |
| 7760 | //TODO: configure audio effect output stage here |
| 7761 | |
| 7762 | // open a duplicating output thread for the new output and the primary output |
| 7763 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 7764 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 7765 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 7766 | if (status == NO_ERROR) { |
| 7767 | // add duplicated output descriptor |
| 7768 | addOutput(duplicatedOutput, dupOutputDesc); |
| 7769 | } else { |
| 7770 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 7771 | mPrimaryOutput->mIoHandle, output); |
| 7772 | desc->close(); |
| 7773 | removeOutput(output); |
| 7774 | nextAudioPortGeneration(); |
| 7775 | return nullptr; |
| 7776 | } |
| 7777 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 7778 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 7779 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 7780 | mPrimaryOutput = desc; |
| 7781 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7782 | return desc; |
| 7783 | } |
| 7784 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 7785 | } // namespace android |