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> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 34 | #include <math.h> |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 35 | #include <set> |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 36 | #include <unordered_set> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 37 | #include <vector> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 38 | |
| 39 | #include <Serializer.h> |
Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 40 | #include <cutils/bitops.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 41 | #include <cutils/properties.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 42 | #include <media/AudioParameter.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 43 | #include <policy.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 44 | #include <private/android_filesystem_config.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 45 | #include <system/audio.h> |
Mikhail Naganov | 27cf37c | 2020-04-14 14:47:01 -0700 | [diff] [blame] | 46 | #include <system/audio_config.h> |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 47 | #include <system/audio_effects/effect_hapticgenerator.h> |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 48 | #include <utils/Log.h> |
| 49 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 50 | #include "AudioPolicyManager.h" |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 51 | #include "TypeConverter.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 52 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 53 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 54 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 55 | //FIXME: workaround for truncated touch sounds |
| 56 | // to be removed when the problem is handled by system UI |
| 57 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 58 | |
| 59 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 60 | // media / notification / system volume. |
| 61 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 62 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 63 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 64 | static const std::vector<audio_format_t> compressedFormatsOrder = {{ |
| 65 | AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 66 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 67 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). |
| 68 | static const std::vector<audio_channel_mask_t> surroundChannelMasksOrder = {{ |
| 69 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 70 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 71 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 72 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 73 | template <typename T> |
| 74 | bool operator== (const SortedVector<T> &left, const SortedVector<T> &right) |
| 75 | { |
| 76 | if (left.size() != right.size()) { |
| 77 | return false; |
| 78 | } |
| 79 | for (size_t index = 0; index < right.size(); index++) { |
| 80 | if (left[index] != right[index]) { |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | template <typename T> |
| 88 | bool operator!= (const SortedVector<T> &left, const SortedVector<T> &right) |
| 89 | { |
| 90 | return !(left == right); |
| 91 | } |
| 92 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 93 | // ---------------------------------------------------------------------------- |
| 94 | // AudioPolicyInterface implementation |
| 95 | // ---------------------------------------------------------------------------- |
| 96 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 97 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 98 | audio_policy_dev_state_t state, |
| 99 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 100 | const char *device_name, |
| 101 | audio_format_t encodedFormat) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 102 | { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 103 | status_t status = setDeviceConnectionStateInt(device, state, device_address, |
| 104 | device_name, encodedFormat); |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 105 | nextAudioPortGeneration(); |
| 106 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 107 | } |
| 108 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 109 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
| 110 | audio_policy_dev_state_t state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 111 | { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 112 | AudioParameter param(String8(device->address().c_str())); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 113 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | f23fcf6 | 2019-07-08 15:28:43 -0700 | [diff] [blame] | 114 | AudioParameter::keyDeviceConnect : AudioParameter::keyDeviceDisconnect); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 115 | param.addInt(key, device->type()); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 116 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 117 | } |
| 118 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 119 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 120 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 121 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 122 | const char *device_name, |
| 123 | audio_format_t encodedFormat) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 124 | { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 125 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s format 0x%X", |
| 126 | deviceType, state, device_address, device_name, encodedFormat); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 127 | |
| 128 | // connect/disconnect only 1 device at a time |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 129 | if (!audio_is_output_device(deviceType) && !audio_is_input_device(deviceType)) return BAD_VALUE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 130 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 131 | sp<DeviceDescriptor> device = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 132 | mHwModules.getDeviceDescriptor(deviceType, device_address, device_name, encodedFormat, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 133 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 134 | return device ? setDeviceConnectionStateInt(device, state) : INVALID_OPERATION; |
| 135 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 136 | |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 137 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, |
| 138 | audio_policy_dev_state_t state) |
| 139 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 140 | // handle output devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 141 | if (audio_is_output_device(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 142 | SortedVector <audio_io_handle_t> outputs; |
| 143 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 144 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 145 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 146 | // save a copy of the opened output descriptors before any output is opened or closed |
| 147 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 148 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 149 | switch (state) |
| 150 | { |
| 151 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 152 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 153 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 154 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 155 | return INVALID_OPERATION; |
| 156 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 157 | ALOGV("%s() connecting device %s format %x", |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 158 | __func__, device->toString().c_str(), device->getEncodedFormat()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 159 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 160 | // register new device as available |
Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 161 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 163 | } |
| 164 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 165 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 166 | // parameters on newly connected devices (instead of opening the outputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 167 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 168 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 169 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 170 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 171 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 172 | mHwModules.cleanUpForDevice(device); |
| 173 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 174 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 175 | return INVALID_OPERATION; |
| 176 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 177 | |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 178 | // Populate encapsulation information when a output device is connected. |
| 179 | device->setEncapsulationInfoFromHal(mpClientInterface); |
| 180 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 181 | // outputs should never be empty here |
| 182 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 183 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 184 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 185 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 186 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 187 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 188 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 189 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 190 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | return INVALID_OPERATION; |
| 192 | } |
| 193 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 194 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 195 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 196 | // Send Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 197 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 198 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 199 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 200 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 202 | mOutputs.clearSessionRoutesForDevice(device); |
| 203 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 204 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 205 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 206 | // Reset active device codec |
| 207 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 208 | |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 209 | // remove device from mReportedFormatsMap cache |
| 210 | mReportedFormatsMap.erase(device); |
| 211 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 212 | } break; |
| 213 | |
| 214 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 215 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 216 | return BAD_VALUE; |
| 217 | } |
| 218 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 219 | // Propagate device availability to Engine |
| 220 | setEngineDeviceConnectionState(device, state); |
| 221 | |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 222 | // No need to evaluate playback routing when connecting a remote submix |
| 223 | // output device used by a dynamic policy of type recorder as no |
| 224 | // playback use case is affected. |
| 225 | bool doCheckForDeviceAndOutputChanges = true; |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 226 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 227 | for (audio_io_handle_t output : outputs) { |
| 228 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 229 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); |
| 230 | if (policyMix != nullptr |
| 231 | && policyMix->mMixType == MIX_TYPE_RECORDERS |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 232 | && device->address() == policyMix->mDeviceAddress.string()) { |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 233 | doCheckForDeviceAndOutputChanges = false; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | auto checkCloseOutputs = [&]() { |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 240 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 241 | if (!outputs.isEmpty()) { |
| 242 | for (audio_io_handle_t output : outputs) { |
| 243 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 244 | // close unused outputs after device disconnection or direct outputs that have |
| 245 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 246 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
| 247 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 248 | (desc->mDirectOpenCount == 0))) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 249 | clearAudioSourcesForOutput(output); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 250 | closeOutput(output); |
| 251 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 252 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 253 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 254 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 255 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 256 | return false; |
Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | if (doCheckForDeviceAndOutputChanges) { |
| 260 | checkForDeviceAndOutputChanges(checkCloseOutputs); |
| 261 | } else { |
| 262 | checkCloseOutputs(); |
| 263 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 264 | (void)updateCallRouting(false /*fromCache*/); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 265 | std::vector<audio_io_handle_t> outputsToReopen; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 266 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 267 | const DeviceVector activeMediaDevices = |
| 268 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 269 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 270 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 271 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || |
| 272 | (desc != mPrimaryOutput))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 273 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 274 | // do not force device change on duplicated output because if device is 0, it will |
| 275 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 276 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 277 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 278 | && !desc->isDuplicated() |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 279 | && (!device_distinguishes_on_address(device->type()) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 280 | // always force when disconnecting (a non-duplicated device) |
| 281 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 282 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 283 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 284 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && |
| 285 | desc->devices() != activeMediaDevices && |
| 286 | desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 287 | // Reopen the output to query the dynamic profiles when there is not active |
| 288 | // clients or all active clients will be rerouted. Otherwise, set the flag |
| 289 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output |
| 290 | // can be reopened to query dynamic profiles when all clients are inactive. |
| 291 | if (areAllActiveTracksRerouted(desc)) { |
| 292 | outputsToReopen.push_back(mOutputs.keyAt(i)); |
| 293 | } else { |
| 294 | desc->mPendingReopenToQueryProfiles = true; |
| 295 | } |
| 296 | } |
| 297 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { |
| 298 | // Clear the flag that previously set for re-querying profiles. |
| 299 | desc->mPendingReopenToQueryProfiles = false; |
| 300 | } |
| 301 | } |
| 302 | for (const auto& output : outputsToReopen) { |
| 303 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 304 | closeOutput(output); |
| 305 | openOutputWithProfileAndDevice(desc->mProfile, activeMediaDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 308 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 309 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 312 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 313 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 314 | } // end if is output device |
| 315 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 316 | // handle input devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 317 | if (audio_is_input_device(device->type())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 318 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 319 | switch (state) |
| 320 | { |
| 321 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 322 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 323 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 324 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 325 | return INVALID_OPERATION; |
| 326 | } |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 327 | |
| 328 | if (mAvailableInputDevices.add(device) < 0) { |
| 329 | return NO_MEMORY; |
| 330 | } |
| 331 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 332 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 333 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 334 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 335 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 336 | if (checkInputsForDevice(device, state) != NO_ERROR) { |
| 337 | mAvailableInputDevices.remove(device); |
| 338 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 339 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 340 | |
| 341 | mHwModules.cleanUpForDevice(device); |
| 342 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 343 | return INVALID_OPERATION; |
| 344 | } |
| 345 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 346 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 347 | |
| 348 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 349 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 350 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 351 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 352 | return INVALID_OPERATION; |
| 353 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 354 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 355 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 356 | |
| 357 | // Set Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 358 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 359 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 360 | mAvailableInputDevices.remove(device); |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 361 | |
| 362 | checkInputsForDevice(device, state); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 363 | |
| 364 | // remove device from mReportedFormatsMap cache |
| 365 | mReportedFormatsMap.erase(device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 366 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 367 | |
| 368 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 369 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 370 | return BAD_VALUE; |
| 371 | } |
| 372 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 373 | // Propagate device availability to Engine |
| 374 | setEngineDeviceConnectionState(device, state); |
| 375 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 376 | checkCloseInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 377 | // As the input device list can impact the output device selection, update |
| 378 | // getDeviceForStrategy() cache |
| 379 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 380 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 381 | (void)updateCallRouting(false /*fromCache*/); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 382 | // Reconnect Audio Source |
| 383 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 384 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 385 | checkAudioSourceForAttributes(attributes); |
| 386 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 387 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 388 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 391 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 392 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 393 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 394 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 395 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 396 | return BAD_VALUE; |
| 397 | } |
| 398 | |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 399 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, |
| 400 | audio_policy_dev_state_t state) { |
| 401 | |
| 402 | // the Engine does not have to know about remote submix devices used by dynamic audio policies |
| 403 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { |
| 404 | return; |
| 405 | } |
| 406 | mEngine->setDeviceConnectionState(device, state); |
| 407 | } |
| 408 | |
| 409 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 410 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 411 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 412 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 413 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 414 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 415 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 416 | (strlen(device_address) != 0)/*matchAddress*/); |
| 417 | |
| 418 | if (devDesc == 0) { |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 419 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 420 | device, device_address); |
| 421 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 422 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 423 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 424 | DeviceVector *deviceVector; |
| 425 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 426 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 427 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 428 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 429 | deviceVector = &mAvailableInputDevices; |
| 430 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 431 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 432 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 433 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 434 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 435 | return (deviceVector->getDevice( |
| 436 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 437 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 440 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 441 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 442 | const char *device_name, |
| 443 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 444 | { |
| 445 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 446 | String8 reply; |
| 447 | AudioParameter param; |
| 448 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 449 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 450 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 451 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 452 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 453 | // connect/disconnect only 1 device at a time |
| 454 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 455 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 456 | // Check if the device is currently connected |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 457 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 458 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 459 | // Nothing to do: device is not connected |
| 460 | return NO_ERROR; |
| 461 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 462 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 463 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 464 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 465 | // configure codecs. |
| 466 | // Handle two specific cases by sending a set parameter to |
| 467 | // configure A2DP codecs. No need to toggle device state. |
| 468 | // Case 1: A2DP active device switches from primary to primary |
| 469 | // module |
| 470 | // Case 2: A2DP device config changes on primary module. |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 471 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 472 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 473 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 474 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 475 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 476 | reply = mpClientInterface->getParameters( |
| 477 | AUDIO_IO_HANDLE_NONE, |
| 478 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 479 | AudioParameter repliedParameters(reply); |
| 480 | repliedParameters.getInt( |
| 481 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 482 | if (isReconfigA2dpSupported) { |
| 483 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 484 | param.add(key, String8("true")); |
| 485 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 486 | devDesc->setEncodedFormat(encodedFormat); |
| 487 | return NO_ERROR; |
| 488 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 491 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 492 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 493 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 494 | // mute media strategies and delay device switch by the largest |
| 495 | // This avoid sending the music tail into the earpiece or headset. |
| 496 | setStrategyMute(musicStrategy, true, desc); |
| 497 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 498 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 499 | nullptr, true /*fromCache*/).types()); |
| 500 | } |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 501 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 502 | // This will force reading again the device configuration |
| 503 | status = setDeviceConnectionState(device, |
| 504 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 505 | device_address, device_name, |
| 506 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 507 | if (status != NO_ERROR) { |
| 508 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 509 | status); |
| 510 | return status; |
| 511 | } |
| 512 | |
| 513 | status = setDeviceConnectionState(device, |
| 514 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 515 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 516 | if (status != NO_ERROR) { |
| 517 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 518 | status); |
| 519 | return status; |
| 520 | } |
| 521 | |
| 522 | return NO_ERROR; |
| 523 | } |
| 524 | |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 525 | status_t AudioPolicyManager::getHwOffloadEncodingFormatsSupportedForA2DP( |
| 526 | std::vector<audio_format_t> *formats) |
| 527 | { |
| 528 | ALOGV("getHwOffloadEncodingFormatsSupportedForA2DP()"); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 529 | status_t status = NO_ERROR; |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 530 | std::unordered_set<audio_format_t> formatSet; |
| 531 | sp<HwModule> primaryModule = |
| 532 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); |
Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 533 | if (primaryModule == nullptr) { |
| 534 | ALOGE("%s() unable to get primary module", __func__); |
| 535 | return NO_INIT; |
| 536 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 537 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( |
| 538 | getAudioDeviceOutAllA2dpSet()); |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 539 | for (const auto& device : declaredDevices) { |
| 540 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 541 | } |
Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 542 | formats->assign(formatSet.begin(), formatSet.end()); |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 543 | return status; |
| 544 | } |
| 545 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 546 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) |
| 547 | { |
| 548 | DeviceVector rxSinkdevices{}; |
| 549 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( |
| 550 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); |
| 551 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { |
| 552 | auto rxSinkDevice = rxSinkdevices.itemAt(0); |
| 553 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( |
| 554 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
| 555 | // retrieve Rx Source device descriptor |
| 556 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( |
| 557 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); |
| 558 | |
| 559 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL |
| 560 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && |
| 561 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { |
| 562 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); |
| 563 | return DeviceVector(rxSinkDevice); |
| 564 | } |
| 565 | } |
| 566 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
| 567 | // the device returned is not necessarily reachable via this output |
| 568 | // (filter later by setOutputDevices()) |
| 569 | return getNewOutputDevices(mPrimaryOutput, fromCache); |
| 570 | } |
| 571 | |
| 572 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) |
| 573 | { |
| 574 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
| 575 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); |
| 576 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); |
| 577 | } |
| 578 | return INVALID_OPERATION; |
| 579 | } |
| 580 | |
| 581 | status_t AudioPolicyManager::updateCallRoutingInternal( |
| 582 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 583 | { |
| 584 | bool createTxPatch = false; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 585 | bool createRxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 586 | uint32_t muteWaitMs = 0; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 587 | if(!hasPrimaryOutput() || |
| 588 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 589 | return INVALID_OPERATION; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 590 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 591 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 592 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 593 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 594 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 595 | ALOG_ASSERT(txSourceDevice != 0, "%s() input selected device not available", __func__); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 596 | |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 597 | ALOGV("%s device rxDevice %s txDevice %s", __func__, |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 598 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 599 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 600 | disconnectTelephonyRxAudioSource(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 601 | // release TX patch if any |
| 602 | if (mCallTxPatch != 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 603 | releaseAudioPatchInternal(mCallTxPatch->getHandle()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 604 | mCallTxPatch.clear(); |
| 605 | } |
| 606 | |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 607 | auto telephonyRxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 608 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 609 | auto telephonyTxModule = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 610 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 611 | // retrieve Rx Source and Tx Sink device descriptors |
| 612 | sp<DeviceDescriptor> rxSourceDevice = |
| 613 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, |
| 614 | String8(), |
| 615 | AUDIO_FORMAT_DEFAULT); |
| 616 | sp<DeviceDescriptor> txSinkDevice = |
| 617 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, |
| 618 | String8(), |
| 619 | AUDIO_FORMAT_DEFAULT); |
| 620 | |
| 621 | // RX and TX Telephony device are declared by Primary Audio HAL |
| 622 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && |
| 623 | (telephonyRxModule->getHalVersionMajor() >= 3)) { |
| 624 | if (rxSourceDevice == 0 || txSinkDevice == 0) { |
| 625 | // RX / TX Telephony device(s) is(are) not currently available |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 626 | ALOGE("%s() no telephony Tx and/or RX device", __func__); |
| 627 | return INVALID_OPERATION; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 628 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 629 | // createAudioPatchInternal now supports both HW / SW bridging |
| 630 | createRxPatch = true; |
| 631 | createTxPatch = true; |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 632 | } else { |
| 633 | // If the RX device is on the primary HW module, then use legacy routing method for |
| 634 | // voice calls via setOutputDevice() on primary output. |
| 635 | // Otherwise, create two audio patches for TX and RX path. |
| 636 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && |
| 637 | (rxSourceDevice != 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 638 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 639 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 640 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && |
| 641 | (txSinkDevice != 0); |
| 642 | } |
| 643 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. |
| 644 | // Otherwise, create two audio patches for TX and RX path. |
| 645 | if (!createRxPatch) { |
| 646 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 647 | } else { // create RX path audio patch |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 648 | connectTelephonyRxAudioSource(); |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 649 | // If the TX device is on the primary HW module but RX device is |
| 650 | // on other HW module, SinkMetaData of telephony input should handle it |
| 651 | // assuming the device uses audio HAL V5.0 and above |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 652 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 653 | if (createTxPatch) { // create TX path audio patch |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 654 | // terminate active capture if on the same HW module as the call TX source device |
| 655 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 656 | // call TX device but this information is not in the audio patch and logic here must be |
| 657 | // symmetric to the one in startInput() |
| 658 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
| 659 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { |
| 660 | closeActiveClients(activeDesc); |
| 661 | } |
| 662 | } |
François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 663 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txSourceDevice, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 664 | } |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 665 | if (waitMs != nullptr) { |
| 666 | *waitMs = muteWaitMs; |
| 667 | } |
| 668 | return NO_ERROR; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 669 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 670 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 671 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 672 | bool isRx, const sp<DeviceDescriptor> &device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 673 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 674 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 675 | if (device == nullptr) { |
| 676 | return nullptr; |
| 677 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 678 | |
| 679 | // @TODO: still ignoring the address, or not dealing platform with multiple telephony devices |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 680 | if (isRx) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 681 | patchBuilder.addSink(device). |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 682 | addSource(mAvailableInputDevices.getDevice( |
| 683 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 684 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 685 | patchBuilder.addSource(device). |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 686 | addSink(mAvailableOutputDevices.getDevice( |
| 687 | AUDIO_DEVICE_OUT_TELEPHONY_TX, String8(), AUDIO_FORMAT_DEFAULT)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 688 | } |
| 689 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 690 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 691 | status_t status = |
| 692 | createAudioPatchInternal(patchBuilder.patch(), &patchHandle, mUidCached, delayMs); |
| 693 | ssize_t index = mAudioPatches.indexOfKey(patchHandle); |
| 694 | if (status != NO_ERROR || index < 0) { |
| 695 | ALOGW("%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 696 | return nullptr; |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 697 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 698 | return mAudioPatches.valueAt(index); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 701 | bool AudioPolicyManager::isDeviceOfModule( |
| 702 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 703 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 704 | if (module != 0) { |
| 705 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 706 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 707 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 708 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 709 | } |
| 710 | return false; |
| 711 | } |
| 712 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 713 | void AudioPolicyManager::connectTelephonyRxAudioSource() |
| 714 | { |
| 715 | disconnectTelephonyRxAudioSource(); |
| 716 | const struct audio_port_config source = { |
| 717 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, |
| 718 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" |
| 719 | }; |
| 720 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); |
| 721 | status_t status = startAudioSource(&source, &aa, &mCallRxSourceClientPort, 0/*uid*/); |
| 722 | ALOGE_IF(status != NO_ERROR, "%s failed to start Telephony Rx AudioSource", __func__); |
| 723 | } |
| 724 | |
| 725 | void AudioPolicyManager::disconnectTelephonyRxAudioSource() |
| 726 | { |
| 727 | stopAudioSource(mCallRxSourceClientPort); |
| 728 | mCallRxSourceClientPort = AUDIO_PORT_HANDLE_NONE; |
| 729 | } |
| 730 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 731 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 732 | { |
| 733 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 734 | // store previous phone state for management of sonification strategy below |
| 735 | int oldState = mEngine->getPhoneState(); |
| 736 | |
| 737 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 738 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 739 | return; |
| 740 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 741 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 742 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 743 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 744 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 745 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 746 | } |
| 747 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 748 | /** |
| 749 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 750 | * routing command. |
| 751 | */ |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 752 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) |
| 753 | || (isStateInCall(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 754 | |
| 755 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 756 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 757 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 758 | int delayMs = 0; |
| 759 | if (isStateInCall(state)) { |
| 760 | nsecs_t sysTime = systemTime(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 761 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); |
| 762 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 763 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 764 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 765 | // mute media and sonification strategies and delay device switch by the largest |
| 766 | // latency of any output where either strategy is active. |
| 767 | // 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] | 768 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || |
| 769 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, |
| 770 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 771 | (delayMs < (int)desc->latency()*2)) { |
| 772 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 773 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 774 | setStrategyMute(musicStrategy, true, desc); |
| 775 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, |
| 776 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 777 | nullptr, true /*fromCache*/).types()); |
| 778 | setStrategyMute(sonificationStrategy, true, desc); |
| 779 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, |
| 780 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), |
| 781 | nullptr, true /*fromCache*/).types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 785 | if (hasPrimaryOutput()) { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 786 | if (state == AUDIO_MODE_IN_CALL) { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 787 | (void)updateCallRouting(false /*fromCache*/, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 788 | } else { |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 789 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 790 | // force routing command to audio hardware when ending call |
| 791 | // even if no device change is needed |
| 792 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 793 | rxDevices = mPrimaryOutput->devices(); |
| 794 | } |
| 795 | if (oldState == AUDIO_MODE_IN_CALL) { |
| 796 | disconnectTelephonyRxAudioSource(); |
| 797 | if (mCallTxPatch != 0) { |
| 798 | releaseAudioPatchInternal(mCallTxPatch->getHandle()); |
| 799 | mCallTxPatch.clear(); |
| 800 | } |
| 801 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 802 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 803 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 804 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 805 | |
| 806 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 807 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 808 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 809 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 810 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 811 | setOutputDevices(desc, newDevices, !newDevices.isEmpty(), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 815 | if (isStateInCall(state)) { |
| 816 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 817 | // force reevaluating accessibility routing when call starts |
| 818 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | // 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] | 822 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && |
| 823 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 826 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 827 | return mEngine->getPhoneState(); |
| 828 | } |
| 829 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 830 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 831 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 832 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 833 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 834 | if (config == mEngine->getForceUse(usage)) { |
| 835 | return; |
| 836 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 837 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 838 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 839 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 840 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 841 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 842 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 843 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 844 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 845 | |
| 846 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 847 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 848 | |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 849 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED |
| 850 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { |
| 851 | mpClientInterface->invalidateStream(AUDIO_STREAM_SYSTEM); |
| 852 | mpClientInterface->invalidateStream(AUDIO_STREAM_ENFORCED_AUDIBLE); |
| 853 | } |
| 854 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 855 | //FIXME: workaround for truncated touch sounds |
| 856 | // to be removed when the problem is handled by system UI |
| 857 | uint32_t delayMs = 0; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 858 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 859 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 860 | } |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 861 | |
| 862 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 863 | updateInputRouting(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 866 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 867 | { |
| 868 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 869 | } |
| 870 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 871 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 872 | // search to profiles for direct outputs. |
| 873 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 874 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 875 | uint32_t samplingRate, |
| 876 | audio_format_t format, |
| 877 | audio_channel_mask_t channelMask, |
| 878 | audio_output_flags_t flags, |
| 879 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 880 | { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 881 | if (directOnly) { |
| 882 | // only retain flags that will drive the direct output profile selection |
| 883 | // if explicitly requested |
| 884 | static const uint32_t kRelevantFlags = |
| 885 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
Aniket Kumar Lata | ba810b8 | 2019-07-03 11:15:33 -0700 | [diff] [blame] | 886 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 887 | flags = |
| 888 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 889 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 890 | |
| 891 | sp<IOProfile> profile; |
| 892 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 893 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 894 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 895 | if (!curProfile->isCompatibleProfile(devices, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 896 | samplingRate, NULL /*updatedSamplingRate*/, |
| 897 | format, NULL /*updatedFormat*/, |
| 898 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 899 | flags)) { |
| 900 | continue; |
| 901 | } |
| 902 | // reject profiles not corresponding to a device currently available |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 903 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 904 | continue; |
| 905 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 906 | // reject profiles if connected device does not support codec |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 907 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 908 | continue; |
| 909 | } |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 910 | if (!directOnly) return curProfile; |
| 911 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 912 | // to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 913 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 914 | continue; |
| 915 | } |
| 916 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 917 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 918 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 919 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 922 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 925 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 926 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 927 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 928 | |
| 929 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 930 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 931 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 932 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 933 | // and AudioSystem::getOutputSamplingRate(). |
| 934 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 935 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 936 | const audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 937 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 938 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 939 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 940 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 943 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 944 | const audio_attributes_t *srcAttr, |
| 945 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 946 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 947 | if (srcAttr != NULL) { |
| 948 | if (!isValidAttributes(srcAttr)) { |
| 949 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 950 | __func__, |
| 951 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 952 | srcAttr->tags); |
| 953 | return BAD_VALUE; |
| 954 | } |
| 955 | *dstAttr = *srcAttr; |
| 956 | } else { |
| 957 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 958 | ALOGE("%s: invalid stream type", __func__); |
| 959 | return BAD_VALUE; |
| 960 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 961 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 962 | } |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 963 | |
| 964 | // Only honor audibility enforced when required. The client will be |
| 965 | // forced to reconnect if the forced usage changes. |
| 966 | 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] | 967 | dstAttr->flags = static_cast<audio_flags_mask_t>( |
| 968 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); |
Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 971 | return NO_ERROR; |
| 972 | } |
| 973 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 974 | status_t AudioPolicyManager::getOutputForAttrInt( |
| 975 | audio_attributes_t *resultAttr, |
| 976 | audio_io_handle_t *output, |
| 977 | audio_session_t session, |
| 978 | const audio_attributes_t *attr, |
| 979 | audio_stream_type_t *stream, |
| 980 | uid_t uid, |
| 981 | const audio_config_t *config, |
| 982 | audio_output_flags_t *flags, |
| 983 | audio_port_handle_t *selectedDeviceId, |
| 984 | bool *isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 985 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 986 | output_type_t *outputType) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 987 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 988 | DeviceVector outputDevices; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 989 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 990 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 991 | const sp<DeviceDescriptor> requestedDevice = |
| 992 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 993 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 994 | *outputType = API_OUTPUT_INVALID; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 995 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 996 | if (status != NO_ERROR) { |
| 997 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 998 | } |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 999 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1000 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1001 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1002 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1003 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1004 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, |
| 1005 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1006 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1007 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, |
| 1008 | // otherwise, fallback to the dynamic policies, if none match, query the engine. |
| 1009 | // 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] | 1010 | sp<AudioPolicyMix> primaryMix; |
| 1011 | status = mPolicyMixes.getOutputForAttr(*resultAttr, uid, *flags, primaryMix, secondaryMixes); |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1012 | if (status != OK) { |
| 1013 | return status; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1014 | } |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1015 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1016 | // Explicit routing is higher priority then any dynamic policy primary output |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1017 | bool usePrimaryOutputFromPolicyMixes = requestedDevice == nullptr && primaryMix != nullptr; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1018 | |
| 1019 | // FIXME: in case of RENDER policy, the output capabilities should be checked |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1020 | if ((usePrimaryOutputFromPolicyMixes |
| 1021 | || (secondaryMixes != nullptr && !secondaryMixes->empty())) |
Kevin Rocard | c2afbdf | 2019-01-31 18:18:06 -0800 | [diff] [blame] | 1022 | && !audio_is_linear_pcm(config->format)) { |
| 1023 | ALOGD("%s: rejecting request as dynamic audio policy only support pcm", __func__); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1024 | return BAD_VALUE; |
| 1025 | } |
| 1026 | if (usePrimaryOutputFromPolicyMixes) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1027 | sp<DeviceDescriptor> deviceDesc = |
| 1028 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, |
| 1029 | primaryMix->mDeviceAddress, |
| 1030 | AUDIO_FORMAT_DEFAULT); |
| 1031 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); |
Eric Laurent | c64e0ab | 2020-04-30 15:59:34 -0700 | [diff] [blame] | 1032 | if (deviceDesc != nullptr |
| 1033 | && (policyDesc == nullptr || (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT))) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1034 | audio_io_handle_t newOutput; |
| 1035 | status = openDirectOutput( |
| 1036 | *stream, session, config, |
| 1037 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), |
| 1038 | DeviceVector(deviceDesc), &newOutput); |
| 1039 | if (status != NO_ERROR) { |
| 1040 | policyDesc = nullptr; |
| 1041 | } else { |
| 1042 | policyDesc = mOutputs.valueFor(newOutput); |
| 1043 | primaryMix->setOutput(policyDesc); |
| 1044 | } |
| 1045 | } |
| 1046 | if (policyDesc != nullptr) { |
| 1047 | policyDesc->mPolicyMix = primaryMix; |
| 1048 | *output = policyDesc->mIoHandle; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1049 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1050 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1051 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 1052 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1053 | *outputType = API_OUT_MIX_PLAYBACK; |
| 1054 | } else { |
| 1055 | *outputType = API_OUTPUT_LEGACY; |
| 1056 | } |
| 1057 | return NO_ERROR; |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1058 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1059 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1060 | // Virtual sources must always be dynamicaly or explicitly routed |
| 1061 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 1062 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 1063 | return BAD_VALUE; |
| 1064 | } |
| 1065 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine |
| 1066 | // in order to let the choice of the order to future vendor engine |
| 1067 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1068 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1069 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1070 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1073 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 1074 | // chosen by the engine if not. |
| 1075 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 1076 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1077 | // 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] | 1078 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1079 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1080 | audio_is_linear_pcm(config->format) && |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1081 | isCallAudioAccessible()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1082 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1083 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1084 | *isRequestedDeviceForExclusiveUse = true; |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1088 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", |
| 1089 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, |
| 1090 | config->channel_mask, *flags, toString(*stream).c_str()); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1091 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1092 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1093 | if (!msdDevices.isEmpty()) { |
| 1094 | *output = getOutputForDevices(msdDevices, session, *stream, config, flags); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1095 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatches(&outputDevices) == NO_ERROR) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1096 | ALOGV("%s() Using MSD devices %s instead of devices %s", |
| 1097 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1098 | } else { |
| 1099 | *output = AUDIO_IO_HANDLE_NONE; |
| 1100 | } |
| 1101 | } |
| 1102 | if (*output == AUDIO_IO_HANDLE_NONE) { |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1103 | *output = getOutputForDevices(outputDevices, session, *stream, config, |
Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 1104 | flags, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1105 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1106 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 1107 | return INVALID_OPERATION; |
| 1108 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1109 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1110 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1111 | for (auto &outputDevice : outputDevices) { |
| 1112 | if (outputDevice->getId() == getConfig().getDefaultOutputDevice()->getId()) { |
| 1113 | *selectedDeviceId = outputDevice->getId(); |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1117 | |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1118 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { |
| 1119 | *outputType = API_OUTPUT_TELEPHONY_TX; |
| 1120 | } else { |
| 1121 | *outputType = API_OUTPUT_LEGACY; |
| 1122 | } |
| 1123 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1124 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 1125 | |
| 1126 | return NO_ERROR; |
| 1127 | } |
| 1128 | |
| 1129 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 1130 | audio_io_handle_t *output, |
| 1131 | audio_session_t session, |
| 1132 | audio_stream_type_t *stream, |
| 1133 | uid_t uid, |
| 1134 | const audio_config_t *config, |
| 1135 | audio_output_flags_t *flags, |
| 1136 | audio_port_handle_t *selectedDeviceId, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1137 | audio_port_handle_t *portId, |
Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1138 | std::vector<audio_io_handle_t> *secondaryOutputs, |
| 1139 | output_type_t *outputType) |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1140 | { |
| 1141 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1142 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1143 | return INVALID_OPERATION; |
| 1144 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1145 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1146 | audio_attributes_t resultAttr; |
François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1147 | bool isRequestedDeviceForExclusiveUse = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1148 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1149 | const sp<DeviceDescriptor> requestedDevice = |
| 1150 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 1151 | |
| 1152 | // Prevent from storing invalid requested device id in clients |
| 1153 | const audio_port_handle_t sanitizedRequestedPortId = |
| 1154 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; |
| 1155 | *selectedDeviceId = sanitizedRequestedPortId; |
| 1156 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1157 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1158 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1159 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1160 | if (status != NO_ERROR) { |
| 1161 | return status; |
| 1162 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1163 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1164 | if (secondaryOutputs != nullptr) { |
| 1165 | for (auto &secondaryMix : secondaryMixes) { |
| 1166 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 1167 | if (outputDesc != nullptr && |
| 1168 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 1169 | secondaryOutputs->push_back(outputDesc->mIoHandle); |
| 1170 | weakSecondaryOutputDescs.push_back(outputDesc); |
| 1171 | } |
| 1172 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1173 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1174 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1175 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1176 | .channel_mask = config->channel_mask, |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1177 | .format = config->format, |
Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1178 | }; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1179 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1180 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1181 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1182 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1183 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1184 | sanitizedRequestedPortId, *stream, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1185 | mEngine->getProductStrategyForAttributes(resultAttr), |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1186 | toVolumeSource(resultAttr), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1187 | *flags, isRequestedDeviceForExclusiveUse, |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1188 | std::move(weakSecondaryOutputDescs), |
| 1189 | outputDesc->mPolicyMix); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1190 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1191 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1192 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, |
| 1193 | *output, requestedPortId, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1194 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1195 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1196 | } |
| 1197 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1198 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, |
| 1199 | audio_session_t session, |
| 1200 | const audio_config_t *config, |
| 1201 | audio_output_flags_t flags, |
| 1202 | const DeviceVector &devices, |
| 1203 | audio_io_handle_t *output) { |
| 1204 | |
| 1205 | *output = AUDIO_IO_HANDLE_NONE; |
| 1206 | |
| 1207 | // skip direct output selection if the request can obviously be attached to a mixed output |
| 1208 | // and not explicitly requested |
| 1209 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 1210 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1211 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
| 1212 | return NAME_NOT_FOUND; |
| 1213 | } |
| 1214 | |
| 1215 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1216 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1217 | // when audioflinger detects there is an active non offloadable effect. |
| 1218 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1219 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1220 | // in the background. |
| 1221 | sp<IOProfile> profile; |
| 1222 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 1223 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
| 1224 | profile = getProfileForOutput( |
| 1225 | devices, config->sample_rate, config->format, config->channel_mask, |
| 1226 | flags, true /* directOnly */); |
| 1227 | } |
| 1228 | |
| 1229 | if (profile == nullptr) { |
| 1230 | return NAME_NOT_FOUND; |
| 1231 | } |
| 1232 | |
| 1233 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1234 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1235 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1236 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1237 | // reuse direct output if currently open by the same client |
| 1238 | // and configured with same parameters |
| 1239 | if ((config->sample_rate == desc->getSamplingRate()) && |
| 1240 | (config->format == desc->getFormat()) && |
| 1241 | (config->channel_mask == desc->getChannelMask()) && |
| 1242 | (session == desc->mDirectClientSession)) { |
| 1243 | desc->mDirectOpenCount++; |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1244 | ALOGV("%s reusing direct output %d for session %d", __func__, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1245 | mOutputs.keyAt(i), session); |
| 1246 | *output = mOutputs.keyAt(i); |
| 1247 | return NO_ERROR; |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | if (!profile->canOpenNewIo()) { |
| 1253 | return NAME_NOT_FOUND; |
| 1254 | } |
| 1255 | |
| 1256 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1257 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 1258 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1259 | // An MSD patch may be using the only output stream that can service this request. Release |
| 1260 | // all MSD patches to prioritize this request over any active output on MSD. |
| 1261 | releaseMsdPatches(devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1262 | |
| 1263 | status_t status = outputDesc->open(config, devices, stream, flags, output); |
| 1264 | |
| 1265 | // only accept an output with the requested parameters |
| 1266 | if (status != NO_ERROR || |
| 1267 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || |
| 1268 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || |
| 1269 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { |
| 1270 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1271 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, |
| 1272 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), |
| 1273 | config->channel_mask, outputDesc->getChannelMask()); |
| 1274 | if (*output != AUDIO_IO_HANDLE_NONE) { |
| 1275 | outputDesc->close(); |
| 1276 | } |
| 1277 | // fall back to mixer output if possible when the direct output could not be open |
| 1278 | if (audio_is_linear_pcm(config->format) && |
| 1279 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
| 1280 | return NAME_NOT_FOUND; |
| 1281 | } |
| 1282 | *output = AUDIO_IO_HANDLE_NONE; |
| 1283 | return BAD_VALUE; |
| 1284 | } |
| 1285 | outputDesc->mDirectOpenCount = 1; |
| 1286 | outputDesc->mDirectClientSession = session; |
| 1287 | |
| 1288 | addOutput(*output, outputDesc); |
| 1289 | mPreviousOutputs = mOutputs; |
| 1290 | ALOGV("%s returns new direct output %d", __func__, *output); |
| 1291 | mpClientInterface->onAudioPortListUpdate(); |
| 1292 | return NO_ERROR; |
| 1293 | } |
| 1294 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1295 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1296 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1297 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1298 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1299 | const audio_config_t *config, |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1300 | audio_output_flags_t *flags, |
| 1301 | bool forceMutingHaptic) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1302 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1303 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1304 | |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1305 | // Discard haptic channel mask when forcing muting haptic channels. |
| 1306 | audio_channel_mask_t channelMask = forceMutingHaptic |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1307 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) |
| 1308 | : config->channel_mask; |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1309 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1310 | // open a direct output if required by specified parameters |
| 1311 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1312 | // and all common behaviors are driven by checking only the direct flag |
| 1313 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1314 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1315 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1316 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1317 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1318 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1319 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1320 | // only allow deep buffering for music stream type |
| 1321 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1322 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1323 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1324 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1325 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1326 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1327 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1328 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1329 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1330 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1331 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1332 | audio_is_linear_pcm(config->format) && |
| 1333 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1334 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1335 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1336 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1337 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1338 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1339 | audio_config_t directConfig = *config; |
| 1340 | directConfig.channel_mask = channelMask; |
| 1341 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); |
| 1342 | if (status != NAME_NOT_FOUND) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1343 | return output; |
| 1344 | } |
| 1345 | |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1346 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1347 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1348 | 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] | 1349 | return AUDIO_IO_HANDLE_NONE; |
| 1350 | } |
| 1351 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1352 | // ignoring channel mask due to downmix capability in mixer |
| 1353 | |
| 1354 | // open a non direct output |
| 1355 | |
| 1356 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1357 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1358 | // get which output is suitable for the specified stream. The actual |
| 1359 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1360 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1361 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1362 | // 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] | 1363 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1364 | output = selectOutput( |
| 1365 | outputs, *flags, config->format, channelMask, config->sample_rate, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1366 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1367 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1368 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1369 | stream, config->sample_rate, config->format, channelMask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1370 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1371 | return output; |
| 1372 | } |
| 1373 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1374 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1375 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1376 | mAvailableInputDevices); |
| 1377 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1378 | } |
| 1379 | |
| 1380 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1381 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1382 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1383 | } |
| 1384 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1385 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1386 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1387 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1388 | if (msdModule != 0) { |
| 1389 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1390 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1391 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1392 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1393 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1394 | source->ext.device.hw_module == msdModule->getHandle()) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1395 | msdPatches.addAudioPatch(patch->getHandle(), patch); |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1396 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | return msdPatches; |
| 1401 | } |
| 1402 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1403 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(const sp<DeviceDescriptor> &outputDevice, |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1404 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1405 | { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1406 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1407 | if (msdModule == nullptr) { |
| 1408 | ALOGE("%s() unable to get MSD module", __func__); |
| 1409 | return NO_INIT; |
| 1410 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1411 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice, AUDIO_FORMAT_DEFAULT); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1412 | if (deviceModule == nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1413 | ALOGE("%s() unable to get module for %s", __func__, outputDevice->toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1414 | return NO_INIT; |
| 1415 | } |
| 1416 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1417 | if (inputProfiles.isEmpty()) { |
| 1418 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1419 | return NO_INIT; |
| 1420 | } |
| 1421 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1422 | if (outputProfiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1423 | ALOGE("%s() no output profiles for device %s", __func__, outputDevice->toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1424 | return NO_INIT; |
| 1425 | } |
| 1426 | AudioProfileVector msdProfiles; |
| 1427 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1428 | for (const auto &inProfile : inputProfiles) { |
| 1429 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 1430 | appendAudioProfiles(msdProfiles, inProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | AudioProfileVector deviceProfiles; |
| 1434 | for (const auto &outProfile : outputProfiles) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1435 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && |
| 1436 | outProfile->supportsDevice(outputDevice)) { |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 1437 | appendAudioProfiles(deviceProfiles, outProfile->getAudioProfiles()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | struct audio_config_base bestSinkConfig; |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 1441 | status_t result = findBestMatchingOutputConfig(msdProfiles, deviceProfiles, |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1442 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 1443 | bestSinkConfig); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1444 | if (result != NO_ERROR) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1445 | ALOGD("%s() no matching profiles found for device: %s, hwAvSync: %d", |
| 1446 | __func__, outputDevice->toString().c_str(), hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1447 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1448 | } |
| 1449 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1450 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1451 | sinkConfig->format = bestSinkConfig.format; |
| 1452 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1453 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1454 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1455 | if (audio_is_iec61937_compatible(sinkConfig->format)) { |
| 1456 | // For formats compatible with IEC61937 encapsulation, assume that |
| 1457 | // the record thread input from MSD is IEC61937 framed (for proportional buffer sizing). |
| 1458 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between |
| 1459 | // raw and IEC61937 framed streams. |
| 1460 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1461 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); |
| 1462 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1463 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1464 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1465 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1466 | sourceConfig->format = bestSinkConfig.format; |
| 1467 | // Copy input stream directly without any processing (e.g. resampling). |
| 1468 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1469 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1470 | if (hwAvSync) { |
| 1471 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1472 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1473 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1474 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1475 | } |
| 1476 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1477 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1478 | sinkConfig->config_mask |= config_mask; |
| 1479 | sourceConfig->config_mask |= config_mask; |
| 1480 | return NO_ERROR; |
| 1481 | } |
| 1482 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1483 | PatchBuilder AudioPolicyManager::buildMsdPatch(const sp<DeviceDescriptor> &outputDevice) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1484 | { |
| 1485 | PatchBuilder patchBuilder; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1486 | patchBuilder.addSource(getMsdAudioInDevice()).addSink(outputDevice); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1487 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1488 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1489 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1490 | // For now, we just forcefully try with HwAvSync first. |
| 1491 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1492 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1493 | getBestMsdAudioProfileFor( |
| 1494 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1495 | if (res == NO_ERROR) { |
| 1496 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1497 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1498 | } |
| 1499 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1500 | " supporting PCM format conversion.", __func__); |
| 1501 | return patchBuilder; |
| 1502 | } |
| 1503 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1504 | status_t AudioPolicyManager::setMsdPatches(const DeviceVector *outputDevices) { |
| 1505 | DeviceVector devices; |
| 1506 | if (outputDevices != nullptr && outputDevices->size() > 0) { |
| 1507 | devices.add(*outputDevices); |
| 1508 | } else { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1509 | // Use media strategy for unspecified output device. This should only |
| 1510 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1511 | // therefore invalidate explicit routing requests. |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1512 | devices = mEngine->getOutputDevicesForAttributes( |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1513 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1514 | 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] | 1515 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1516 | std::vector<PatchBuilder> patchesToCreate; |
| 1517 | for (auto i = 0u; i < devices.size(); ++i) { |
| 1518 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); |
| 1519 | patchesToCreate.push_back(buildMsdPatch(devices[i])); |
| 1520 | } |
| 1521 | // Retain only the MSD patches associated with outputDevices request. |
| 1522 | // Tear down the others, and create new ones as needed. |
| 1523 | AudioPatchCollection patchesToRemove = getMsdPatches(); |
| 1524 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { |
| 1525 | auto retainedPatch = false; |
| 1526 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1527 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { |
| 1528 | patchesToRemove.removeItemsAt(i); |
| 1529 | retainedPatch = true; |
| 1530 | break; |
| 1531 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1532 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1533 | if (retainedPatch) { |
| 1534 | it = patchesToCreate.erase(it); |
| 1535 | continue; |
| 1536 | } |
| 1537 | ++it; |
| 1538 | } |
| 1539 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { |
| 1540 | return NO_ERROR; |
| 1541 | } |
| 1542 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { |
| 1543 | auto ¤tPatch = patchesToRemove.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1544 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1545 | } |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1546 | status_t status = NO_ERROR; |
| 1547 | for (const auto &p : patchesToCreate) { |
| 1548 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1549 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1550 | char message[256]; |
| 1551 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " |
| 1552 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1553 | currStatus == NO_ERROR ? "Success" : "Error", |
| 1554 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, |
| 1555 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); |
| 1556 | if (currStatus == NO_ERROR) { |
| 1557 | ALOGD("%s", message); |
| 1558 | } else { |
| 1559 | ALOGE("%s", message); |
| 1560 | if (status == NO_ERROR) { |
| 1561 | status = currStatus; |
| 1562 | } |
| 1563 | } |
| 1564 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1565 | return status; |
| 1566 | } |
| 1567 | |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1568 | void AudioPolicyManager::releaseMsdPatches(const DeviceVector& devices) { |
| 1569 | AudioPatchCollection msdPatches = getMsdPatches(); |
| 1570 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1571 | const auto& patch = msdPatches[i]; |
| 1572 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1573 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1574 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, |
| 1575 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { |
| 1576 | releaseAudioPatch(patch->getHandle(), mUidCached); |
| 1577 | break; |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1583 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1584 | audio_output_flags_t flags, |
| 1585 | audio_format_t format, |
| 1586 | audio_channel_mask_t channelMask, |
| 1587 | uint32_t samplingRate, |
| 1588 | audio_session_t sessionId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1589 | { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1590 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), |
| 1591 | "%s called with format %#x", __func__, format); |
| 1592 | |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1593 | // Return the output that haptic-generating attached to when 1) session id is specified, |
| 1594 | // 2) haptic-generating effect exists for given session id and 3) the output that |
| 1595 | // haptic-generating effect attached to is in given outputs. |
| 1596 | if (sessionId != AUDIO_SESSION_NONE) { |
| 1597 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( |
| 1598 | sessionId, FX_IID_HAPTICGENERATOR); |
| 1599 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { |
| 1600 | return hapticGeneratingOutput; |
| 1601 | } |
| 1602 | } |
| 1603 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1604 | // Flags disqualifying an output: the match must happen before calling selectOutput() |
| 1605 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) |
| 1606 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1607 | |
| 1608 | // Flags expressing a functional request: must be honored in priority over |
| 1609 | // other criteria |
| 1610 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) |
| 1611 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | |
| 1612 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM); |
| 1613 | // Flags expressing a performance request: have lower priority than serving |
| 1614 | // requested sampling rate or channel mask |
| 1615 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) |
| 1616 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | |
| 1617 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); |
| 1618 | |
| 1619 | const audio_output_flags_t functionalFlags = |
| 1620 | (audio_output_flags_t)(flags & kFunctionalFlags); |
| 1621 | const audio_output_flags_t performanceFlags = |
| 1622 | (audio_output_flags_t)(flags & kPerformanceFlags); |
| 1623 | |
| 1624 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; |
| 1625 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1626 | // 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] | 1627 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1628 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1629 | // 1: the output supporting haptic playback when requesting haptic playback |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1630 | // 2: the output with the highest number of requested functional flags |
| 1631 | // 3: the output supporting the exact channel mask |
| 1632 | // 4: the output with a higher channel count than requested |
| 1633 | // 5: the output with a higher sampling rate than requested |
| 1634 | // 6: the output with the highest number of requested performance flags |
| 1635 | // 7: the output with the bit depth the closest to the requested one |
| 1636 | // 8: the primary output |
| 1637 | // 9: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1638 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1639 | // matching criteria values in priority order for best matching output so far |
| 1640 | std::vector<uint32_t> bestMatchCriteria(8, 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1641 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1642 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); |
| 1643 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1644 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1645 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1646 | for (audio_io_handle_t output : outputs) { |
| 1647 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1648 | // matching criteria values in priority order for current output |
| 1649 | std::vector<uint32_t> currentMatchCriteria(8, 0); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1650 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1651 | if (outputDesc->isDuplicated()) { |
| 1652 | continue; |
| 1653 | } |
| 1654 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { |
| 1655 | continue; |
| 1656 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1657 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1658 | // If haptic channel is specified, use the haptic output if present. |
| 1659 | // When using haptic output, same audio format and sample rate are required. |
| 1660 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1661 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1662 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { |
| 1663 | continue; |
| 1664 | } |
| 1665 | if (outputHapticChannelCount >= hapticChannelCount |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1666 | && format == outputDesc->getFormat() |
| 1667 | && samplingRate == outputDesc->getSamplingRate()) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1668 | currentMatchCriteria[0] = outputHapticChannelCount; |
| 1669 | } |
| 1670 | |
| 1671 | // functional flags match |
| 1672 | currentMatchCriteria[1] = popcount(outputDesc->mFlags & functionalFlags); |
| 1673 | |
| 1674 | // channel mask and channel count match |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1675 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( |
| 1676 | outputDesc->getChannelMask()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1677 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && |
| 1678 | channelCount <= outputChannelCount) { |
| 1679 | if ((audio_channel_mask_get_representation(channelMask) == |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1680 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && |
| 1681 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1682 | currentMatchCriteria[2] = outputChannelCount; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1683 | } |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1684 | currentMatchCriteria[3] = outputChannelCount; |
| 1685 | } |
| 1686 | |
| 1687 | // sampling rate match |
| 1688 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT && |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1689 | samplingRate <= outputDesc->getSamplingRate()) { |
| 1690 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | // performance flags match |
| 1694 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); |
| 1695 | |
| 1696 | // format match |
| 1697 | if (format != AUDIO_FORMAT_INVALID) { |
| 1698 | currentMatchCriteria[6] = |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1699 | PolicyAudioPort::kFormatDistanceMax - |
| 1700 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | // primary output match |
| 1704 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; |
| 1705 | |
| 1706 | // compare match criteria by priority then value |
| 1707 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 1708 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { |
| 1709 | bestMatchCriteria = currentMatchCriteria; |
| 1710 | bestOutput = output; |
| 1711 | |
| 1712 | std::stringstream result; |
| 1713 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), |
| 1714 | std::ostream_iterator<int>(result, " ")); |
| 1715 | ALOGV("%s new bestOutput %d criteria %s", |
| 1716 | __func__, bestOutput, result.str().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1717 | } |
| 1718 | } |
| 1719 | |
Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1720 | return bestOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1723 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1724 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1725 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1726 | |
| 1727 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1728 | if (outputDesc == 0) { |
| 1729 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1730 | return BAD_VALUE; |
| 1731 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1732 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1733 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1734 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1735 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1736 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1737 | status_t status = outputDesc->start(); |
| 1738 | if (status != NO_ERROR) { |
| 1739 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1740 | } |
| 1741 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1742 | uint32_t delayMs; |
| 1743 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1744 | |
| 1745 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1746 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1747 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1748 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1749 | if (delayMs != 0) { |
| 1750 | usleep(delayMs * 1000); |
| 1751 | } |
| 1752 | |
| 1753 | return status; |
| 1754 | } |
| 1755 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1756 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1757 | const sp<TrackClientDescriptor>& client, |
| 1758 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1759 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1760 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1761 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1762 | |
| 1763 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1764 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1765 | auto clientVolSrc = client->volumeSource(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1766 | auto clientStrategy = client->strategy(); |
| 1767 | auto clientAttr = client->attributes(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1768 | if (stream == AUDIO_STREAM_TTS) { |
| 1769 | ALOGV("\t found BEACON stream"); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1770 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 1771 | toVolumeSource(AUDIO_STREAM_TTS) /*sourceToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1772 | return INVALID_OPERATION; |
| 1773 | } else { |
| 1774 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1775 | } |
| 1776 | } else { |
| 1777 | // some playback other than beacon starts |
| 1778 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1779 | } |
| 1780 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1781 | // 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] | 1782 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1783 | bool force = !outputDesc->isActive() && |
| 1784 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1785 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1786 | DeviceVector devices; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 1787 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1788 | const char *address = NULL; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1789 | if (policyMix != nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1790 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1791 | address = policyMix->mDeviceAddress.string(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1792 | 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] | 1793 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1794 | } else { |
| 1795 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1796 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1797 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), |
| 1798 | AUDIO_FORMAT_DEFAULT); |
| 1799 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); |
| 1800 | devices.add(device); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1803 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1804 | // It covers a common case when there is no materially active audio |
| 1805 | // and muting would result in unnecessary delay and dropped audio. |
| 1806 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1807 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1808 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1809 | // increment usage count for this stream on the requested output: |
| 1810 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1811 | // 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] | 1812 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1813 | |
| 1814 | if (client->hasPreferredDevice(true)) { |
François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 1815 | if (outputDesc->clientsList(true /*activeOnly*/).size() == 1 && |
| 1816 | client->isPreferredDeviceForExclusiveUse()) { |
| 1817 | // Preferred device may be exclusive, use only if no other active clients on this output |
| 1818 | devices = DeviceVector( |
| 1819 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); |
| 1820 | } else { |
| 1821 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 1822 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1823 | if (devices != outputDesc->devices()) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1824 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1825 | } |
| 1826 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1827 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1828 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1829 | selectOutputForMusicEffects(); |
| 1830 | } |
| 1831 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1832 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1833 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1834 | if (devices.isEmpty()) { |
| 1835 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1836 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1837 | bool shouldWait = |
| 1838 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || |
| 1839 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || |
| 1840 | (beaconMuteLatency > 0)); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1841 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1842 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1843 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1844 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1845 | // An output has a shared device if |
| 1846 | // - managed by the same hw module |
| 1847 | // - supports the currently selected device |
| 1848 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1849 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1850 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1851 | // force a device change if any other output is: |
| 1852 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1853 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1854 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1855 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1856 | // 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] | 1857 | // change the device currently selected by the other output. |
| 1858 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1859 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1860 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1861 | force = true; |
| 1862 | } |
| 1863 | // 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] | 1864 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1865 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1866 | const uint32_t latencyMs = desc->latency(); |
| 1867 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1868 | |
| 1869 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1870 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1871 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1872 | |
| 1873 | // Require mute check if another output is on a shared device |
| 1874 | // and currently active to have proper drain and avoid pops. |
| 1875 | // Note restoring AudioTracks onto this output needs to invoke |
| 1876 | // a volume ramp if there is no mute. |
| 1877 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1878 | } |
| 1879 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1880 | |
| 1881 | const uint32_t muteWaitMs = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1882 | setOutputDevices(outputDesc, devices, force, 0, NULL, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1883 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1884 | // apply volume rules for current stream and device if necessary |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1885 | auto &curves = getVolumeCurves(client->attributes()); |
| 1886 | checkAndSetVolume(curves, client->volumeSource(), |
| 1887 | curves.getVolumeIndex(outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1888 | outputDesc, |
Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 1889 | outputDesc->devices().types(), 0 /*delay*/, |
| 1890 | outputDesc->useHwGain() /*force*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1891 | |
| 1892 | // update the outputs if starting an output with a stream that can affect notification |
| 1893 | // routing |
| 1894 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1895 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1896 | // force reevaluating accessibility routing when ringtone or alarm starts |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1897 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1898 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1899 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1900 | |
| 1901 | if (waitMs > muteWaitMs) { |
| 1902 | *delayMs = waitMs - muteWaitMs; |
| 1903 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1904 | |
| 1905 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1906 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1907 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1908 | // change occurs after the MixerThread starts and causes a stream volume |
| 1909 | // glitch. |
| 1910 | // |
| 1911 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1912 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1913 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1914 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1915 | mEngine->getForceUse( |
| 1916 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1917 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1918 | } |
| 1919 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1920 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1921 | // of type MIX_TYPE_RECORDERS |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1922 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && |
| 1923 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1924 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1925 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1926 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1927 | "remote-submix", |
| 1928 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1931 | return NO_ERROR; |
| 1932 | } |
| 1933 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1934 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1935 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1936 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1937 | |
| 1938 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1939 | if (outputDesc == 0) { |
| 1940 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1941 | return BAD_VALUE; |
| 1942 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1943 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1944 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1945 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1946 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1947 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1948 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1949 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1950 | if (status == NO_ERROR ) { |
| 1951 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1952 | } |
| 1953 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1956 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1957 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1958 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1959 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1960 | audio_stream_type_t stream = client->stream(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1961 | auto clientVolSrc = client->volumeSource(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1962 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1963 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1964 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1965 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { |
| 1966 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1967 | // Automatically disable the remote submix input when output is stopped on a |
| 1968 | // re routing mix of type MIX_TYPE_RECORDERS |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 1969 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1970 | if (isSingleDeviceType( |
| 1971 | outputDesc->devices().types(), &audio_is_remote_submix_device) && |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1972 | policyMix != nullptr && |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 1973 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1974 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1975 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 1976 | policyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1977 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | bool forceDeviceUpdate = false; |
| 1981 | if (client->hasPreferredDevice(true)) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1982 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1983 | forceDeviceUpdate = true; |
| 1984 | } |
| 1985 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1986 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1987 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1988 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1989 | // store time at which the stream was stopped - see isStreamActive() |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1990 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1991 | outputDesc->setStopTime(client, systemTime()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1992 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1993 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1994 | // the track stop() command is received and at that time the audio track buffer can |
| 1995 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1996 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1997 | // audio path (audio DSP, CODEC ...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1998 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1999 | |
| 2000 | // force restoring the device selection on other active outputs if it differs from the |
| 2001 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2002 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2003 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2004 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2005 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2006 | desc->isActive() && |
| 2007 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2008 | (newDevices != desc->devices())) { |
| 2009 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 2010 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2011 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2012 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 2013 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2014 | // re-apply device specific volume if not done by setOutputDevice() |
| 2015 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2016 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2017 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | // update the outputs if stopping one with a stream that can affect notification routing |
| 2021 | handleNotificationRoutingForStream(stream); |
| 2022 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2023 | |
| 2024 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 2025 | 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] | 2026 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2027 | } |
| 2028 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2029 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2030 | selectOutputForMusicEffects(); |
| 2031 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2032 | return NO_ERROR; |
| 2033 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2034 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2035 | return INVALID_OPERATION; |
| 2036 | } |
| 2037 | } |
| 2038 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2039 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2040 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2041 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2042 | |
| 2043 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 2044 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2045 | // If an output descriptor is closed due to a device routing change, |
| 2046 | // then there are race conditions with releaseOutput from tracks |
| 2047 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 2048 | // destroyed shortly thereafter. |
| 2049 | // |
| 2050 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2051 | ALOGW("releaseOutput() no output for client %d", portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2052 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2053 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2054 | |
| 2055 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2056 | |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2057 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
| 2058 | if (outputDesc->isClientActive(client)) { |
| 2059 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); |
| 2060 | stopOutput(portId); |
| 2061 | } |
| 2062 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2063 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 2064 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2065 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2066 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2067 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2068 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2069 | if (--outputDesc->mDirectOpenCount == 0) { |
| 2070 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2071 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2072 | } |
| 2073 | } |
Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2074 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2075 | outputDesc->removeClient(portId); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2076 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { |
| 2077 | // The output is pending reopened to query dynamic profiles and |
| 2078 | // there is no active clients |
| 2079 | closeOutput(outputDesc->mIoHandle); |
| 2080 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( |
| 2081 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); |
| 2082 | if (newOutputDesc == nullptr) { |
| 2083 | ALOGE("%s failed to open output", __func__); |
| 2084 | } |
| 2085 | return true; |
| 2086 | } |
| 2087 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2090 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 2091 | audio_io_handle_t *input, |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2092 | audio_unique_id_t riid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2093 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2094 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2095 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2096 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2097 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2098 | input_type_t *inputType, |
| 2099 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2100 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2101 | ALOGV("%s() source %d, sampling rate %d, format %#x, channel mask %#x, session %d, " |
| 2102 | "flags %#x attributes=%s", __func__, attr->source, config->sample_rate, |
| 2103 | config->format, config->channel_mask, session, flags, toString(*attr).c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2104 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2105 | status_t status = NO_ERROR; |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 2106 | audio_source_t halInputSource; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2107 | audio_attributes_t attributes = *attr; |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2108 | sp<AudioPolicyMix> policyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2109 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2110 | sp<AudioInputDescriptor> inputDesc; |
| 2111 | sp<RecordClientDescriptor> clientDesc; |
| 2112 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2113 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2114 | |
| 2115 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 2116 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 2117 | return INVALID_OPERATION; |
| 2118 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2119 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2120 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 2121 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2122 | } |
| 2123 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2124 | // Explicit routing? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2125 | sp<DeviceDescriptor> explicitRoutingDevice = |
| 2126 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2127 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2128 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 2129 | // possible |
| 2130 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 2131 | *input != AUDIO_IO_HANDLE_NONE) { |
| 2132 | ssize_t index = mInputs.indexOfKey(*input); |
| 2133 | if (index < 0) { |
| 2134 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 2135 | status = BAD_VALUE; |
| 2136 | goto error; |
| 2137 | } |
| 2138 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2139 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 2140 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2141 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 2142 | status = BAD_VALUE; |
| 2143 | goto error; |
| 2144 | } |
| 2145 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 2146 | // 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] | 2147 | // corresponds to a new client and is only permitted from the same UID. |
| 2148 | // 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] | 2149 | if (clients.size() > 1) { |
| 2150 | for (const auto& client : clients) { |
| 2151 | // The client map is ordered by key values (portId) and portIds are allocated |
| 2152 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 2153 | // when the mmap stream is created and should be ignored as it does not correspond |
| 2154 | // to an actual client |
| 2155 | if (client == *clients.cbegin()) { |
| 2156 | continue; |
| 2157 | } |
| 2158 | if (uid != client->uid() && !client->isSilenced()) { |
| 2159 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 2160 | uid, client->portId(), client->uid()); |
| 2161 | status = INVALID_OPERATION; |
| 2162 | goto error; |
| 2163 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2164 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2165 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2166 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2167 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2168 | |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2169 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2170 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | *input = AUDIO_IO_HANDLE_NONE; |
| 2174 | *inputType = API_INPUT_INVALID; |
| 2175 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2176 | halInputSource = attributes.source; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2177 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2178 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
| 2179 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { |
| 2180 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2181 | if (status != NO_ERROR) { |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2182 | ALOGW("%s could not find input mix for attr %s", |
| 2183 | __func__, toString(attributes).c_str()); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2184 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2185 | } |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2186 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2187 | String8(attr->tags + strlen("addr=")), |
| 2188 | AUDIO_FORMAT_DEFAULT); |
| 2189 | if (device == nullptr) { |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2190 | 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] | 2191 | __func__, attributes.source, attributes.tags); |
| 2192 | status = BAD_VALUE; |
| 2193 | goto error; |
| 2194 | } |
| 2195 | |
Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2196 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { |
| 2197 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; |
| 2198 | } else { |
| 2199 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 2200 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2201 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2202 | if (explicitRoutingDevice != nullptr) { |
| 2203 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2204 | } else { |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2205 | // Prevent from storing invalid requested device id in clients |
| 2206 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2207 | device = mEngine->getInputDeviceForAttributes(attributes, &policyMix); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2208 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2209 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2210 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2211 | status = BAD_VALUE; |
| 2212 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2213 | } |
Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2214 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { |
| 2215 | *inputType = API_INPUT_MIX_CAPTURE; |
| 2216 | } else if (policyMix) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2217 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 2218 | // there is an external policy, but this input is attached to a mix of recorders, |
| 2219 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 2220 | // know about it and is therefore considered "legacy" |
| 2221 | *inputType = API_INPUT_LEGACY; |
| 2222 | } else if (audio_is_remote_submix_device(device->type())) { |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2223 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2224 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2225 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2226 | } else { |
| 2227 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2228 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2229 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2232 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2233 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2234 | status = INVALID_OPERATION; |
| 2235 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2236 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2237 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2238 | exit: |
| 2239 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2240 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 2241 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2242 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2243 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2244 | mSoundTriggerSessions.indexOfKey(session) >= 0; |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2245 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2246 | |
Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2247 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2248 | requestedDeviceId, attributes.source, flags, |
| 2249 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2250 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2251 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2252 | |
| 2253 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 2254 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2255 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2256 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2257 | |
| 2258 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2259 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2263 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2264 | audio_session_t session, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2265 | const audio_attributes_t &attributes, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2266 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2267 | audio_input_flags_t flags, |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2268 | const sp<AudioPolicyMix> &policyMix) |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2269 | { |
| 2270 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2271 | audio_source_t halInputSource = attributes.source; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2272 | bool isSoundTrigger = false; |
| 2273 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2274 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2275 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2276 | if (index >= 0) { |
| 2277 | input = mSoundTriggerSessions.valueFor(session); |
| 2278 | isSoundTrigger = true; |
| 2279 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 2280 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 2281 | } else { |
| 2282 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2283 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2284 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2285 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2286 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2287 | } |
| 2288 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2289 | // find a compatible input profile (not necessarily identical in parameters) |
| 2290 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2291 | // sampling rate and flags may be updated by getInputProfile |
| 2292 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 2293 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 2294 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2295 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2296 | audio_input_flags_t profileFlags = flags; |
| 2297 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 2298 | 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] | 2299 | profile = getInputProfile(device, profileSamplingRate, profileFormat, profileChannelMask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2300 | profileFlags); |
| 2301 | if (profile != 0) { |
| 2302 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2303 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 2304 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2305 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 2306 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 2307 | } else { // fail |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2308 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 2309 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 2310 | config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2311 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2312 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2314 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2315 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2316 | if (samplingRate == 0) { |
| 2317 | samplingRate = profileSamplingRate; |
| 2318 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2319 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2320 | if (profile->getModuleHandle() == 0) { |
| 2321 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2322 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2323 | } |
| 2324 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2325 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2326 | for (size_t i = 0; i < mInputs.size(); ) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2327 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2328 | if (desc->mProfile != profile) { |
Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2329 | i++; |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2330 | continue; |
| 2331 | } |
| 2332 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2333 | // else |
| 2334 | // reuse input if active client app is not in IDLE state |
| 2335 | // |
| 2336 | RecordClientVector clients = desc->clientsList(); |
| 2337 | bool doClose = false; |
| 2338 | for (const auto& client : clients) { |
| 2339 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2340 | continue; |
| 2341 | } |
| 2342 | if (client->isSoundTrigger()) { |
| 2343 | if (session == client->session()) { |
| 2344 | return desc->mIoHandle; |
| 2345 | } |
| 2346 | continue; |
| 2347 | } |
| 2348 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2349 | return desc->mIoHandle; |
| 2350 | } |
| 2351 | doClose = true; |
| 2352 | } |
| 2353 | if (doClose) { |
| 2354 | closeInput(desc->mIoHandle); |
| 2355 | } else { |
| 2356 | i++; |
| 2357 | } |
| 2358 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2359 | } |
| 2360 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2361 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2362 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2363 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2364 | lConfig.sample_rate = profileSamplingRate; |
| 2365 | lConfig.channel_mask = profileChannelMask; |
| 2366 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2367 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2368 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2369 | |
| 2370 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2371 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2372 | (profileSamplingRate != lConfig.sample_rate) || |
| 2373 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2374 | (profileChannelMask != lConfig.channel_mask)) { |
| 2375 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2376 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2377 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2378 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2379 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2380 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2381 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2384 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2385 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2386 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2387 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2388 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2389 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2392 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2393 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2394 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2395 | |
| 2396 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2397 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2398 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2399 | return DEAD_OBJECT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2400 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2401 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2402 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2403 | if (client->active()) { |
| 2404 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2405 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2408 | audio_session_t session = client->session(); |
| 2409 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2410 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2411 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2412 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2413 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2414 | status_t status = inputDesc->start(); |
| 2415 | if (status != NO_ERROR) { |
| 2416 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2417 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2418 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2419 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2420 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2421 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2422 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2423 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2424 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2425 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2426 | if (device != nullptr) { |
| 2427 | status = setInputDevice(input, device, true /* force */); |
| 2428 | } else { |
| 2429 | ALOGW("%s no new input device can be found for descriptor %d", |
| 2430 | __FUNCTION__, inputDesc->getId()); |
| 2431 | status = BAD_VALUE; |
| 2432 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2433 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2434 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2435 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2436 | // 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] | 2437 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2438 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2439 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2440 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2441 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2442 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2443 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2444 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2445 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2446 | mpClientInterface->setSoundTriggerCaptureState(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2447 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2448 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2449 | // automatically enable the remote submix output when input is started if not |
| 2450 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2451 | // 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] | 2452 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2453 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2454 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2455 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2456 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2457 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2458 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2459 | if (address != "") { |
| 2460 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2461 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2462 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2463 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2464 | } |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2465 | } else if (status != NO_ERROR) { |
| 2466 | // Restore client activity state. |
| 2467 | inputDesc->setClientActive(client, false); |
| 2468 | inputDesc->stop(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2471 | ALOGV("%s input %d source = %d status = %d exit", |
| 2472 | __FUNCTION__, input, client->source(), status); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2473 | |
Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2474 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2477 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2478 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2479 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2480 | |
| 2481 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2482 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2483 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2484 | return BAD_VALUE; |
| 2485 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2486 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2487 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2488 | if (!client->active()) { |
| 2489 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2490 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2493 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2494 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2495 | inputDesc->stop(); |
| 2496 | if (inputDesc->isActive()) { |
| 2497 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2498 | } else { |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2499 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2500 | // 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] | 2501 | if ((policyMix != nullptr) |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2502 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2503 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2504 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2505 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2506 | |
| 2507 | // automatically disable the remote submix output when input is stopped if not |
| 2508 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2509 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2510 | String8 address = String8(""); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2511 | if (policyMix == nullptr) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2512 | address = String8("0"); |
Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2513 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2514 | address = policyMix->mDeviceAddress; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2515 | } |
| 2516 | if (address != "") { |
| 2517 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2518 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2519 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2520 | } |
| 2521 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2522 | resetInputDevice(input); |
| 2523 | |
| 2524 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2525 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2526 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2527 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2528 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2529 | mpClientInterface->setSoundTriggerCaptureState(false); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2530 | } |
| 2531 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2532 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2533 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2536 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2537 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2538 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2539 | |
| 2540 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2541 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2542 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2543 | return; |
| 2544 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2545 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2546 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2547 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2548 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2549 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2550 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2551 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2552 | if (inputDesc->getClientCount() > 0) { |
| 2553 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2554 | return; |
| 2555 | } |
| 2556 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2557 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2558 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2559 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2562 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2563 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2564 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2565 | |
| 2566 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2567 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2568 | } |
| 2569 | } |
| 2570 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2571 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2572 | { |
| 2573 | stopInput(portId); |
| 2574 | releaseInput(portId); |
| 2575 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2576 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2577 | void AudioPolicyManager::checkCloseInputs() { |
| 2578 | // After connecting or disconnecting an input device, close input if: |
| 2579 | // - it has no client (was just opened to check profile) OR |
| 2580 | // - none of its supported devices are connected anymore OR |
| 2581 | // - one of its clients cannot be routed to one of its supported |
| 2582 | // devices anymore. Otherwise update device selection |
| 2583 | std::vector<audio_io_handle_t> inputsToClose; |
| 2584 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2585 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); |
| 2586 | if (input->clientsList().size() == 0 |
Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 2587 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2588 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 2589 | } else { |
| 2590 | bool close = false; |
| 2591 | for (const auto& client : input->clientsList()) { |
| 2592 | sp<DeviceDescriptor> device = |
| 2593 | mEngine->getInputDeviceForAttributes(client->attributes()); |
| 2594 | if (!input->supportedDevices().contains(device)) { |
| 2595 | close = true; |
| 2596 | break; |
| 2597 | } |
| 2598 | } |
| 2599 | if (close) { |
| 2600 | inputsToClose.push_back(mInputs.keyAt(i)); |
| 2601 | } else { |
| 2602 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | for (const audio_io_handle_t handle : inputsToClose) { |
| 2608 | ALOGV("%s closing input %d", __func__, handle); |
| 2609 | closeInput(handle); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2610 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2611 | } |
| 2612 | |
François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 2613 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2614 | { |
| 2615 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 2616 | if (indexMin < 0 || indexMax < 0) { |
| 2617 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); |
| 2618 | return; |
| 2619 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2620 | getVolumeCurves(stream).initVolume(indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2621 | |
| 2622 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2623 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2624 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2625 | continue; |
| 2626 | } |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2627 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2628 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2629 | } |
| 2630 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2631 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2632 | int index, |
| 2633 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2634 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2635 | auto attributes = mEngine->getAttributesForStreamType(stream); |
Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 2636 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { |
| 2637 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); |
| 2638 | return NO_ERROR; |
| 2639 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2640 | ALOGV("%s: stream %s attributes=%s", __func__, |
| 2641 | toString(stream).c_str(), toString(attributes).c_str()); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2642 | return setVolumeIndexForAttributes(attributes, index, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2643 | } |
| 2644 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2645 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2646 | int *index, |
| 2647 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2648 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2649 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 2650 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2651 | DeviceTypeSet deviceTypes = {device}; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2652 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2653 | deviceTypes = mEngine->getOutputDevicesForStream( |
| 2654 | stream, true /*fromCache*/).types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2655 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2656 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2659 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2660 | int index, |
| 2661 | audio_devices_t device) |
| 2662 | { |
| 2663 | // Get Volume group matching the Audio Attributes |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2664 | auto group = mEngine->getVolumeGroupForAttributes(attributes); |
| 2665 | if (group == VOLUME_GROUP_NONE) { |
| 2666 | 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] | 2667 | return BAD_VALUE; |
| 2668 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2669 | 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] | 2670 | status_t status = NO_ERROR; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2671 | IVolumeCurves &curves = getVolumeCurves(attributes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2672 | VolumeSource vs = toVolumeSource(group); |
| 2673 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); |
| 2674 | |
| 2675 | status = setVolumeCurveIndex(index, device, curves); |
| 2676 | if (status != NO_ERROR) { |
| 2677 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); |
| 2678 | return status; |
| 2679 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2680 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2681 | DeviceTypeSet curSrcDevices; |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2682 | auto curCurvAttrs = curves.getAttributes(); |
| 2683 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { |
| 2684 | auto attr = curCurvAttrs.front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2685 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2686 | } else if (!curves.getStreamTypes().empty()) { |
| 2687 | auto stream = curves.getStreamTypes().front(); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2688 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2689 | } else { |
| 2690 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); |
| 2691 | return BAD_VALUE; |
| 2692 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2693 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); |
| 2694 | resetDeviceTypes(curSrcDevices, curSrcDevice); |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2695 | |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2696 | // update volume on all outputs and streams matching the following: |
| 2697 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2698 | // - The device (or devices) selected by the engine for this stream includes |
| 2699 | // the requested device |
| 2700 | // - For non default requested device, currently selected device on the output is either the |
| 2701 | // requested device or one of the devices selected by the engine for this stream |
| 2702 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2703 | // no specific device volume value exists for currently selected device. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2704 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2705 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2706 | DeviceTypeSet curDevices = desc->devices().types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2707 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2708 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { |
| 2709 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); |
Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 2710 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2711 | if (!(desc->isActive(vs) || isInCall())) { |
| 2712 | continue; |
| 2713 | } |
| 2714 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && |
| 2715 | curDevices.find(device) == curDevices.end()) { |
| 2716 | continue; |
| 2717 | } |
| 2718 | bool applyVolume = false; |
| 2719 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
| 2720 | curSrcDevices.insert(device); |
| 2721 | applyVolume = (curSrcDevices.find( |
| 2722 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); |
| 2723 | } else { |
| 2724 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); |
| 2725 | } |
| 2726 | if (!applyVolume) { |
| 2727 | continue; // next output |
| 2728 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2729 | // Inter / intra volume group priority management: Loop on strategies arranged by priority |
| 2730 | // If a higher priority strategy is active, and the output is routed to a device with a |
| 2731 | // HW Gain management, do not change the volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2732 | if (desc->useHwGain()) { |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2733 | applyVolume = false; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2734 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 2735 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, |
| 2736 | false /*preferredDevice*/); |
| 2737 | if (activeClients.empty()) { |
| 2738 | continue; |
| 2739 | } |
| 2740 | bool isPreempted = false; |
| 2741 | bool isHigherPriority = productStrategy < strategy; |
| 2742 | for (const auto &client : activeClients) { |
| 2743 | if (isHigherPriority && (client->volumeSource() != vs)) { |
| 2744 | ALOGV("%s: Strategy=%d (\nrequester:\n" |
| 2745 | " group %d, volumeGroup=%d attributes=%s)\n" |
| 2746 | " higher priority source active:\n" |
| 2747 | " volumeGroup=%d attributes=%s) \n" |
| 2748 | " on output %zu, bailing out", __func__, productStrategy, |
| 2749 | group, group, toString(attributes).c_str(), |
| 2750 | client->volumeSource(), toString(client->attributes()).c_str(), i); |
| 2751 | applyVolume = false; |
| 2752 | isPreempted = true; |
| 2753 | break; |
| 2754 | } |
| 2755 | // However, continue for loop to ensure no higher prio clients running on output |
| 2756 | if (client->volumeSource() == vs) { |
| 2757 | applyVolume = true; |
| 2758 | } |
| 2759 | } |
| 2760 | if (isPreempted || applyVolume) { |
| 2761 | break; |
| 2762 | } |
| 2763 | } |
| 2764 | if (!applyVolume) { |
| 2765 | continue; // next output |
| 2766 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2767 | } |
François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2768 | //FIXME: workaround for truncated touch sounds |
| 2769 | // delayed volume change for system stream to be removed when the problem is |
| 2770 | // handled by system UI |
| 2771 | status_t volStatus = checkAndSetVolume( |
| 2772 | curves, vs, index, desc, curDevices, |
| 2773 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM))? |
| 2774 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); |
| 2775 | if (volStatus != NO_ERROR) { |
| 2776 | status = volStatus; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2777 | } |
| 2778 | } |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2779 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); |
| 2780 | return status; |
| 2781 | } |
| 2782 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2783 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2784 | audio_devices_t device, |
| 2785 | IVolumeCurves &volumeCurves) |
| 2786 | { |
| 2787 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2788 | // app that has MODIFY_PHONE_STATE permission. |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2789 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); |
| 2790 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2791 | (index > volumeCurves.getVolumeIndexMax())) { |
| 2792 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, |
| 2793 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); |
| 2794 | return BAD_VALUE; |
| 2795 | } |
| 2796 | if (!audio_is_output_device(device)) { |
| 2797 | return BAD_VALUE; |
| 2798 | } |
| 2799 | |
| 2800 | // Force max volume if stream cannot be muted |
| 2801 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); |
| 2802 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2803 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2804 | volumeCurves.addCurrentVolumeIndex(device, index); |
| 2805 | return NO_ERROR; |
| 2806 | } |
| 2807 | |
| 2808 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 2809 | int &index, |
| 2810 | audio_devices_t device) |
| 2811 | { |
| 2812 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this |
| 2813 | // stream by the engine. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2814 | DeviceTypeSet deviceTypes = {device}; |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2815 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2816 | DeviceTypeSet deviceTypes = mEngine->getOutputDevicesForAttributes( |
| 2817 | attr, nullptr, true /*fromCache*/).types(); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2818 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2819 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, |
| 2823 | int &index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2824 | const DeviceTypeSet& deviceTypes) const |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2825 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2826 | if (isSingleDeviceType(deviceTypes, audio_is_output_device)) { |
François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2827 | return BAD_VALUE; |
| 2828 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2829 | index = curves.getVolumeIndex(deviceTypes); |
| 2830 | 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] | 2831 | return NO_ERROR; |
| 2832 | } |
| 2833 | |
| 2834 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 2835 | int &index) |
| 2836 | { |
| 2837 | index = getVolumeCurves(attr).getVolumeIndexMin(); |
| 2838 | return NO_ERROR; |
| 2839 | } |
| 2840 | |
| 2841 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, |
| 2842 | int &index) |
| 2843 | { |
| 2844 | index = getVolumeCurves(attr).getVolumeIndexMax(); |
| 2845 | return NO_ERROR; |
| 2846 | } |
| 2847 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2848 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2849 | { |
| 2850 | // select one output among several suitable for global effects. |
| 2851 | // The priority is as follows: |
| 2852 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2853 | // AudioFlinger will invalidate the track and the offloaded output |
| 2854 | // will be closed causing the effect to be moved to a PCM output. |
| 2855 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2856 | // 3: The primary output |
| 2857 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2858 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2859 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( |
| 2860 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2861 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2862 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2863 | if (outputs.size() == 0) { |
| 2864 | return AUDIO_IO_HANDLE_NONE; |
| 2865 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2866 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2867 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2868 | bool activeOnly = true; |
| 2869 | |
| 2870 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2871 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2872 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2873 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2874 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2875 | for (audio_io_handle_t output : outputs) { |
| 2876 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 2877 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2878 | continue; |
| 2879 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2880 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2881 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2882 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2883 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2884 | } |
| 2885 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2886 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2887 | } |
| 2888 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2889 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2890 | } |
| 2891 | } |
| 2892 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2893 | output = outputOffloaded; |
| 2894 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2895 | output = outputDeepBuffer; |
| 2896 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2897 | output = outputPrimary; |
| 2898 | } else { |
| 2899 | output = outputs[0]; |
| 2900 | } |
| 2901 | activeOnly = false; |
| 2902 | } |
| 2903 | |
| 2904 | if (output != mMusicEffectOutput) { |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2905 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2906 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2907 | mMusicEffectOutput = output; |
| 2908 | } |
| 2909 | |
| 2910 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2911 | return output; |
| 2912 | } |
| 2913 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2914 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2915 | { |
| 2916 | return selectOutputForMusicEffects(); |
| 2917 | } |
| 2918 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2919 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2920 | audio_io_handle_t io, |
Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2921 | product_strategy_t strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2922 | int session, |
| 2923 | int id) |
| 2924 | { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 2925 | if (session != AUDIO_SESSION_DEVICE) { |
| 2926 | ssize_t index = mOutputs.indexOfKey(io); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2927 | if (index < 0) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 2928 | index = mInputs.indexOfKey(io); |
| 2929 | if (index < 0) { |
| 2930 | ALOGW("registerEffect() unknown io %d", io); |
| 2931 | return INVALID_OPERATION; |
| 2932 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2933 | } |
| 2934 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2935 | return mEffects.registerEffect(desc, io, session, id, |
| 2936 | (strategy == streamToStrategy(AUDIO_STREAM_MUSIC) || |
| 2937 | strategy == PRODUCT_STRATEGY_NONE)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 2940 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 2941 | { |
| 2942 | if (mEffects.getEffect(id) == nullptr) { |
| 2943 | return INVALID_OPERATION; |
| 2944 | } |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 2945 | if (mEffects.isEffectEnabled(id)) { |
| 2946 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 2947 | setEffectEnabled(id, false); |
| 2948 | } |
| 2949 | return mEffects.unregisterEffect(id); |
| 2950 | } |
| 2951 | |
| 2952 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 2953 | { |
| 2954 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 2955 | if (effect == nullptr) { |
| 2956 | return INVALID_OPERATION; |
| 2957 | } |
| 2958 | |
| 2959 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 2960 | if (status == NO_ERROR) { |
| 2961 | mInputs.trackEffectEnabled(effect, enabled); |
| 2962 | } |
| 2963 | return status; |
| 2964 | } |
| 2965 | |
Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2966 | |
| 2967 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) |
| 2968 | { |
| 2969 | mEffects.moveEffects(ids, io); |
| 2970 | return NO_ERROR; |
| 2971 | } |
| 2972 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2973 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2974 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2975 | return mOutputs.isActive(toVolumeSource(stream), inPastMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2979 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2980 | return mOutputs.isActiveRemotely(toVolumeSource(stream), inPastMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2981 | } |
| 2982 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2983 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2984 | { |
| 2985 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2986 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2987 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2988 | return true; |
| 2989 | } |
| 2990 | } |
| 2991 | return false; |
| 2992 | } |
| 2993 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2994 | // Register a list of custom mixes with their attributes and format. |
| 2995 | // When a mix is registered, corresponding input and output profiles are |
| 2996 | // added to the remote submix hw module. The profile contains only the |
| 2997 | // parameters (sampling rate, format...) specified by the mix. |
| 2998 | // The corresponding input remote submix device is also connected. |
| 2999 | // |
| 3000 | // When a remote submix device is connected, the address is checked to select the |
| 3001 | // appropriate profile and the corresponding input or output stream is opened. |
| 3002 | // |
| 3003 | // When capture starts, getInputForAttr() will: |
| 3004 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3005 | // - 2 if none found, getDeviceForInputSource() will: |
| 3006 | // - 2.1 look for a mix matching the attributes source |
| 3007 | // - 2.2 if none found, default to device selection by policy rules |
| 3008 | // At this time, the corresponding output remote submix device is also connected |
| 3009 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 3010 | // after AudioTracks are invalidated |
| 3011 | // |
| 3012 | // When playback starts, getOutputForAttr() will: |
| 3013 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 3014 | // - 2 if none found, look for a mix matching the attributes usage |
| 3015 | // - 3 if none found, default to device and output selection by policy rules. |
| 3016 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3017 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3018 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3019 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 3020 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3021 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3022 | sp<HwModule> rSubmixModule; |
| 3023 | // examine each mix's route type |
| 3024 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3025 | AudioMix mix = mixes[i]; |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3026 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode |
| 3027 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { |
| 3028 | ALOGE("Unsupported Policy Mix %zu of %zu: " |
| 3029 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", |
| 3030 | i, mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3031 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3032 | break; |
| 3033 | } |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3034 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled |
| 3035 | // in the same way. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3036 | 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] | 3037 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), |
| 3038 | mix.mRouteFlags); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3039 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3040 | rSubmixModule = mHwModules.getModuleFromName( |
| 3041 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3042 | if (rSubmixModule == 0) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3043 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3044 | i); |
| 3045 | res = INVALID_OPERATION; |
| 3046 | break; |
| 3047 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3048 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3049 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3050 | String8 address = mix.mDeviceAddress; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3051 | audio_devices_t deviceTypeToMakeAvailable; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3052 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3053 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3054 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3055 | } else { |
| 3056 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 3057 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3058 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3059 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3060 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3061 | ALOGE("Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3062 | res = INVALID_OPERATION; |
| 3063 | break; |
| 3064 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3065 | audio_config_t outputConfig = mix.mFormat; |
| 3066 | audio_config_t inputConfig = mix.mFormat; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3067 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL |
| 3068 | // 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] | 3069 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3070 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3071 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3072 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3073 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3074 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3075 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3076 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, |
jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3077 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3078 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { |
| 3079 | ALOGE("Failed to set remote submix device available, type %u, address %s", |
| 3080 | mix.mDeviceType, address.string()); |
| 3081 | break; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3082 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3083 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 3084 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3085 | audio_devices_t type = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3086 | 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] | 3087 | i, mixes.size(), type, address.string()); |
| 3088 | |
| 3089 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( |
| 3090 | mix.mDeviceType, mix.mDeviceAddress, |
| 3091 | String8(), AUDIO_FORMAT_DEFAULT); |
| 3092 | if (device == nullptr) { |
| 3093 | res = INVALID_OPERATION; |
| 3094 | break; |
| 3095 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3096 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3097 | bool foundOutput = false; |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3098 | // First try to find an already opened output supporting the device |
| 3099 | 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] | 3100 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3101 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3102 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3103 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3104 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3105 | address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3106 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3107 | } else { |
| 3108 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3109 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3110 | } |
| 3111 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3112 | // If no output found, try to find a direct output profile supporting the device |
| 3113 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { |
| 3114 | sp<HwModule> module = mHwModules[i]; |
| 3115 | for (size_t j = 0; |
| 3116 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; |
| 3117 | j++) { |
| 3118 | sp<IOProfile> profile = module->getOutputProfiles()[j]; |
| 3119 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { |
| 3120 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { |
| 3121 | ALOGE("Could not register mix RENDER, dev=0x%X addr=%s", type, |
| 3122 | address.string()); |
| 3123 | res = INVALID_OPERATION; |
| 3124 | } else { |
| 3125 | foundOutput = true; |
| 3126 | } |
| 3127 | } |
| 3128 | } |
| 3129 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3130 | if (res != NO_ERROR) { |
| 3131 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3132 | i, type, address.string()); |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3133 | res = INVALID_OPERATION; |
| 3134 | break; |
| 3135 | } else if (!foundOutput) { |
| 3136 | 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] | 3137 | i, type, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3138 | res = INVALID_OPERATION; |
| 3139 | break; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3140 | } else { |
| 3141 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3142 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3143 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3144 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3145 | if (res != NO_ERROR) { |
| 3146 | unregisterPolicyMixes(mixes); |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3147 | } else if (checkOutputs) { |
| 3148 | checkForDeviceAndOutputChanges(); |
| 3149 | updateCallAndOutputRouting(); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3150 | } |
| 3151 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 3155 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3156 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3157 | status_t res = NO_ERROR; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3158 | bool checkOutputs = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3159 | sp<HwModule> rSubmixModule; |
| 3160 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3161 | for (const auto& mix : mixes) { |
| 3162 | 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] | 3163 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3164 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3165 | rSubmixModule = mHwModules.getModuleFromName( |
| 3166 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 3167 | if (rSubmixModule == 0) { |
| 3168 | res = INVALID_OPERATION; |
| 3169 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3170 | } |
| 3171 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3172 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3173 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3174 | |
Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3175 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3176 | res = INVALID_OPERATION; |
| 3177 | continue; |
| 3178 | } |
| 3179 | |
Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3180 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { |
| 3181 | if (getDeviceConnectionState(device, address.string()) == |
| 3182 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3183 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3184 | address.string(), "remote-submix", |
| 3185 | AUDIO_FORMAT_DEFAULT); |
| 3186 | if (res != OK) { |
| 3187 | ALOGE("Error making RemoteSubmix device unavailable for mix " |
| 3188 | "with type %d, address %s", device, address.string()); |
| 3189 | } |
| 3190 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3191 | } |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3192 | rSubmixModule->removeOutputProfile(address.c_str()); |
| 3193 | rSubmixModule->removeInputProfile(address.c_str()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3194 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3195 | } 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] | 3196 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3197 | res = INVALID_OPERATION; |
| 3198 | continue; |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3199 | } else { |
| 3200 | checkOutputs = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3201 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3202 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3203 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3204 | if (res == NO_ERROR && checkOutputs) { |
| 3205 | checkForDeviceAndOutputChanges(); |
| 3206 | updateCallAndOutputRouting(); |
| 3207 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3208 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3209 | } |
| 3210 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3211 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 3212 | { |
| 3213 | size_t i = 0; |
| 3214 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 3215 | for (const auto& fmt : mManualSurroundFormats) { |
| 3216 | if (i++ != 0) dst->append(", "); |
| 3217 | std::string sfmt; |
| 3218 | FormatConverter::toString(fmt, sfmt); |
| 3219 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 3220 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 3221 | } |
| 3222 | } |
| 3223 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3224 | // Returns true if all devices types match the predicate and are supported by one HW module |
| 3225 | bool AudioPolicyManager::areAllDevicesSupported( |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3226 | const AudioDeviceTypeAddrVector& devices, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3227 | std::function<bool(audio_devices_t)> predicate, |
| 3228 | const char *context) { |
| 3229 | for (size_t i = 0; i < devices.size(); i++) { |
| 3230 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3231 | devices[i].mType, devices[i].getAddress(), String8(), |
Eric Laurent | 0e26e3f | 2020-04-29 14:24:16 -0700 | [diff] [blame] | 3232 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, true /*matchAddress*/); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3233 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3234 | ALOGE("%s: device type %#x address %s not supported or not match predicate", |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3235 | context, devices[i].mType, devices[i].getAddress()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3236 | return false; |
| 3237 | } |
| 3238 | } |
| 3239 | return true; |
| 3240 | } |
| 3241 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3242 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3243 | const AudioDeviceTypeAddrVector& devices) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3244 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3245 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3246 | return BAD_VALUE; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3247 | } |
| 3248 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3249 | if (res != NO_ERROR) { |
| 3250 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); |
| 3251 | return res; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3252 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3253 | |
| 3254 | checkForDeviceAndOutputChanges(); |
| 3255 | updateCallAndOutputRouting(); |
| 3256 | |
| 3257 | return NO_ERROR; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 3261 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3262 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); |
| 3263 | if (res != NO_ERROR) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3264 | ALOGE("%s() Could not remove all device affinities for uid = %d", |
Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3265 | __FUNCTION__, uid); |
| 3266 | return INVALID_OPERATION; |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3267 | } |
| 3268 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3269 | checkForDeviceAndOutputChanges(); |
| 3270 | updateCallAndOutputRouting(); |
| 3271 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3272 | return res; |
| 3273 | } |
| 3274 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3275 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3276 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, |
| 3277 | device_role_t role, |
| 3278 | const AudioDeviceTypeAddrVector &devices) { |
| 3279 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, |
| 3280 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3281 | |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3282 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3283 | return BAD_VALUE; |
| 3284 | } |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3285 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3286 | if (status != NO_ERROR) { |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3287 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", |
| 3288 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3289 | return status; |
| 3290 | } |
| 3291 | |
| 3292 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3293 | |
| 3294 | bool forceVolumeReeval = false; |
| 3295 | // FIXME: workaround for truncated touch sounds |
| 3296 | // to be removed when the problem is handled by system UI |
| 3297 | uint32_t delayMs = 0; |
| 3298 | if (strategy == mCommunnicationStrategy) { |
| 3299 | forceVolumeReeval = true; |
| 3300 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3301 | updateInputRouting(); |
| 3302 | } |
| 3303 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3304 | |
| 3305 | return NO_ERROR; |
| 3306 | } |
| 3307 | |
| 3308 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs) |
| 3309 | { |
| 3310 | uint32_t waitMs = 0; |
Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame^] | 3311 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3312 | // Only apply special touch sound delay once |
| 3313 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3314 | } |
| 3315 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3316 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 3317 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
| 3318 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
| 3319 | // As done in setDeviceConnectionState, we could also fix default device issue by |
| 3320 | // preventing the force re-routing in case of default dev that distinguishes on address. |
| 3321 | // Let's give back to engine full device choice decision however. |
| 3322 | waitMs = setOutputDevices(outputDesc, newDevices, !newDevices.isEmpty(), delayMs); |
Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3323 | // Only apply special touch sound delay once |
| 3324 | delayMs = 0; |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3325 | } |
| 3326 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 3327 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
| 3328 | } |
| 3329 | } |
| 3330 | } |
| 3331 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3332 | void AudioPolicyManager::updateInputRouting() { |
| 3333 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3334 | // Skip for hotword recording as the input device switch |
| 3335 | // is handled within sound trigger HAL |
| 3336 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { |
| 3337 | continue; |
| 3338 | } |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3339 | auto newDevice = getNewInputDevice(activeDesc); |
| 3340 | // Force new input selection if the new device can not be reached via current input |
| 3341 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
| 3342 | setInputDevice(activeDesc->mIoHandle, newDevice); |
| 3343 | } else { |
| 3344 | closeInput(activeDesc->mIoHandle); |
| 3345 | } |
| 3346 | } |
| 3347 | } |
| 3348 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3349 | status_t AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, |
| 3350 | device_role_t role) |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3351 | { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3352 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3353 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3354 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3355 | if (status != NO_ERROR) { |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3356 | ALOGV("Engine could not remove preferred device for strategy %d status %d", |
| 3357 | strategy, status); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3358 | return status; |
| 3359 | } |
| 3360 | |
| 3361 | checkForDeviceAndOutputChanges(); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3362 | |
| 3363 | bool forceVolumeReeval = false; |
| 3364 | // FIXME: workaround for truncated touch sounds |
| 3365 | // to be removed when the problem is handled by system UI |
| 3366 | uint32_t delayMs = 0; |
| 3367 | if (strategy == mCommunnicationStrategy) { |
| 3368 | forceVolumeReeval = true; |
| 3369 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 3370 | updateInputRouting(); |
| 3371 | } |
| 3372 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3373 | |
| 3374 | return NO_ERROR; |
| 3375 | } |
| 3376 | |
jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3377 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, |
| 3378 | device_role_t role, |
| 3379 | AudioDeviceTypeAddrVector &devices) { |
| 3380 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3381 | } |
| 3382 | |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3383 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( |
| 3384 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3385 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3386 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3387 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3388 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3389 | return BAD_VALUE; |
| 3390 | } |
| 3391 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3392 | ALOGW_IF(status != NO_ERROR, |
| 3393 | "Engine could not set preferred devices %s for audio source %d role %d", |
| 3394 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3395 | |
| 3396 | return status; |
| 3397 | } |
| 3398 | |
| 3399 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( |
| 3400 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { |
| 3401 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, |
| 3402 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3403 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3404 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3405 | return BAD_VALUE; |
| 3406 | } |
| 3407 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); |
| 3408 | ALOGW_IF(status != NO_ERROR, |
| 3409 | "Engine could not add preferred devices %s for audio source %d role %d", |
| 3410 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); |
| 3411 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3412 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3413 | return status; |
| 3414 | } |
| 3415 | |
| 3416 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( |
| 3417 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) |
| 3418 | { |
| 3419 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, |
| 3420 | dumpAudioDeviceTypeAddrVector(devices).c_str()); |
| 3421 | |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3422 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3423 | return BAD_VALUE; |
| 3424 | } |
| 3425 | |
| 3426 | status_t status = mEngine->removeDevicesRoleForCapturePreset( |
| 3427 | audioSource, role, devices); |
| 3428 | ALOGW_IF(status != NO_ERROR, |
| 3429 | "Engine could not remove devices role (%d) for capture preset %d", role, audioSource); |
| 3430 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3431 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3432 | return status; |
| 3433 | } |
| 3434 | |
| 3435 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, |
| 3436 | device_role_t role) { |
| 3437 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); |
| 3438 | |
| 3439 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); |
| 3440 | ALOGW_IF(status != NO_ERROR, |
| 3441 | "Engine could not clear devices role (%d) for capture preset %d", role, audioSource); |
| 3442 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3443 | updateInputRouting(); |
Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3444 | return status; |
| 3445 | } |
| 3446 | |
| 3447 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( |
| 3448 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { |
| 3449 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); |
| 3450 | } |
| 3451 | |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3452 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, |
jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3453 | const AudioDeviceTypeAddrVector& devices) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3454 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3455 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { |
| 3456 | return BAD_VALUE; |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3457 | } |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3458 | status_t status = mPolicyMixes.setUserIdDeviceAffinities(userId, devices); |
| 3459 | if (status != NO_ERROR) { |
| 3460 | ALOGE("%s() could not set device affinity for userId %d", |
| 3461 | __FUNCTION__, userId); |
| 3462 | return status; |
| 3463 | } |
| 3464 | |
| 3465 | // reevaluate outputs for all devices |
| 3466 | checkForDeviceAndOutputChanges(); |
| 3467 | updateCallAndOutputRouting(); |
| 3468 | |
| 3469 | return NO_ERROR; |
| 3470 | } |
| 3471 | |
| 3472 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3473 | ALOGV("%s() userId=%d", __FUNCTION__, userId); |
Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3474 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); |
| 3475 | if (status != NO_ERROR) { |
| 3476 | ALOGE("%s() Could not remove all device affinities fo userId = %d", |
| 3477 | __FUNCTION__, userId); |
| 3478 | return status; |
| 3479 | } |
| 3480 | |
| 3481 | // reevaluate outputs for all devices |
| 3482 | checkForDeviceAndOutputChanges(); |
| 3483 | updateCallAndOutputRouting(); |
| 3484 | |
| 3485 | return NO_ERROR; |
| 3486 | } |
| 3487 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3488 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3489 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3490 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 3491 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3492 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 3493 | std::string stateLiteral; |
| 3494 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3495 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3496 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 3497 | "communications", "media", "record", "dock", "system", |
| 3498 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 3499 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 3500 | 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] | 3501 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 3502 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 3503 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 3504 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3505 | dst->append(" (MANUAL: "); |
| 3506 | dumpManualSurroundFormats(dst); |
| 3507 | dst->append(")"); |
| 3508 | } |
| 3509 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3510 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3511 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 3512 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3513 | dst->appendFormat(" Communnication Strategy: %d\n", mCommunnicationStrategy); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3514 | 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] | 3515 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3516 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 3517 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 3518 | mHwModulesAll.dump(dst); |
| 3519 | mOutputs.dump(dst); |
| 3520 | mInputs.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3521 | mEffects.dump(dst); |
| 3522 | mAudioPatches.dump(dst); |
| 3523 | mPolicyMixes.dump(dst); |
| 3524 | mAudioSources.dump(dst); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3525 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3526 | dst->appendFormat(" AllowedCapturePolicies:\n"); |
| 3527 | for (auto& policy : mAllowedCapturePolicies) { |
| 3528 | dst->appendFormat(" - uid=%d flag_mask=%#x\n", policy.first, policy.second); |
| 3529 | } |
| 3530 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3531 | dst->appendFormat("\nPolicy Engine dump:\n"); |
| 3532 | mEngine->dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | status_t AudioPolicyManager::dump(int fd) |
| 3536 | { |
| 3537 | String8 result; |
| 3538 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 3539 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3540 | return NO_ERROR; |
| 3541 | } |
| 3542 | |
Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3543 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) |
| 3544 | { |
| 3545 | mAllowedCapturePolicies[uid] = capturePolicy; |
| 3546 | return NO_ERROR; |
| 3547 | } |
| 3548 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3549 | // This function checks for the parameters which can be offloaded. |
| 3550 | // This can be enhanced depending on the capability of the DSP and policy |
| 3551 | // of the system. |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3552 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3553 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3554 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3555 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3556 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3557 | offloadInfo.format, |
| 3558 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 3559 | offloadInfo.has_video); |
| 3560 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3561 | if (mMasterMono) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3562 | return AUDIO_OFFLOAD_NOT_SUPPORTED; // no offloading if mono is set. |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3563 | } |
| 3564 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3565 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 3566 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3567 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); |
| 3568 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | // Check if stream type is music, then only allow offload as of now. |
| 3572 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 3573 | { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3574 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); |
| 3575 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 3579 | const bool allowOffloadWithVideo = |
| 3580 | property_get_bool("audio.offload.video", false /* default_value */); |
| 3581 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3582 | ALOGV("%s: has_video == true, returning false", __func__); |
| 3583 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 3587 | const int min_duration_secs = property_get_int32( |
| 3588 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 3589 | if (min_duration_secs >= 0) { |
| 3590 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3591 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 3592 | __func__, min_duration_secs); |
| 3593 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3594 | } |
| 3595 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3596 | ALOGV("%s: Offload denied by duration < default min(=%u)", |
| 3597 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 3598 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 3602 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 3603 | // detects there is an active non offloadable effect. |
| 3604 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 3605 | // This may prevent offloading in rare situations where effects are left active by apps |
| 3606 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 3607 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3608 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | // See if there is a profile to support this. |
| 3612 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3613 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3614 | offloadInfo.sample_rate, |
| 3615 | offloadInfo.format, |
| 3616 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3617 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 3618 | true /* directOnly */); |
Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 3619 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", |
| 3620 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) |
| 3621 | ? ", supports gapless" : ""); |
Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3622 | if (profile == nullptr) { |
| 3623 | return AUDIO_OFFLOAD_NOT_SUPPORTED; |
| 3624 | } |
| 3625 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { |
| 3626 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; |
| 3627 | } |
| 3628 | return AUDIO_OFFLOAD_SUPPORTED; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3629 | } |
| 3630 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3631 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 3632 | const audio_attributes_t& attributes) { |
| 3633 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 3634 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3635 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3636 | config.sample_rate, |
| 3637 | config.format, |
| 3638 | config.channel_mask, |
| 3639 | output_flags, |
| 3640 | true /* directOnly */); |
| 3641 | ALOGV("%s() profile %sfound with name: %s, " |
| 3642 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 3643 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3644 | (profile != 0 ? profile->getTagName().c_str() : "null"), |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3645 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 3646 | return (profile != 0); |
| 3647 | } |
| 3648 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3649 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 3650 | audio_port_type_t type, |
| 3651 | unsigned int *num_ports, |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 3652 | struct audio_port_v7 *ports, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3653 | unsigned int *generation) |
| 3654 | { |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 3655 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || |
| 3656 | generation == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3657 | return BAD_VALUE; |
| 3658 | } |
| 3659 | 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] | 3660 | if (ports == nullptr) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3661 | *num_ports = 0; |
| 3662 | } |
| 3663 | |
| 3664 | size_t portsWritten = 0; |
| 3665 | size_t portsMax = *num_ports; |
| 3666 | *num_ports = 0; |
| 3667 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3668 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 3669 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3670 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3671 | for (const auto& dev : mAvailableOutputDevices) { |
| 3672 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3673 | continue; |
| 3674 | } |
| 3675 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3676 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3677 | } |
| 3678 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3679 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3680 | } |
| 3681 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3682 | for (const auto& dev : mAvailableInputDevices) { |
| 3683 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3684 | continue; |
| 3685 | } |
| 3686 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3687 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3688 | } |
| 3689 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3690 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3691 | } |
| 3692 | } |
| 3693 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 3694 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 3695 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 3696 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 3697 | } |
| 3698 | *num_ports += mInputs.size(); |
| 3699 | } |
| 3700 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3701 | size_t numOutputs = 0; |
| 3702 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3703 | if (!mOutputs[i]->isDuplicated()) { |
| 3704 | numOutputs++; |
| 3705 | if (portsWritten < portsMax) { |
| 3706 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 3707 | } |
| 3708 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3709 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3710 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3711 | } |
| 3712 | } |
| 3713 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3714 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3715 | return NO_ERROR; |
| 3716 | } |
| 3717 | |
jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 3718 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3719 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 3720 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 3721 | return BAD_VALUE; |
| 3722 | } |
| 3723 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 3724 | if (dev != 0) { |
| 3725 | dev->toAudioPort(port); |
| 3726 | return NO_ERROR; |
| 3727 | } |
| 3728 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 3729 | if (dev != 0) { |
| 3730 | dev->toAudioPort(port); |
| 3731 | return NO_ERROR; |
| 3732 | } |
| 3733 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 3734 | if (out != 0) { |
| 3735 | out->toAudioPort(port); |
| 3736 | return NO_ERROR; |
| 3737 | } |
| 3738 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 3739 | if (in != 0) { |
| 3740 | in->toAudioPort(port); |
| 3741 | return NO_ERROR; |
| 3742 | } |
| 3743 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3746 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, |
| 3747 | audio_patch_handle_t *handle, |
| 3748 | uid_t uid, uint32_t delayMs, |
| 3749 | const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3750 | { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3751 | ALOGV("%s", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3752 | if (handle == NULL || patch == NULL) { |
| 3753 | return BAD_VALUE; |
| 3754 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3755 | 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] | 3756 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 3757 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3758 | return BAD_VALUE; |
| 3759 | } |
| 3760 | // only one source per audio patch supported for now |
| 3761 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3762 | return INVALID_OPERATION; |
| 3763 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3764 | |
| 3765 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3766 | return INVALID_OPERATION; |
| 3767 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3768 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3769 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 3770 | return INVALID_OPERATION; |
| 3771 | } |
| 3772 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3773 | |
| 3774 | sp<AudioPatch> patchDesc; |
| 3775 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 3776 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3777 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, |
| 3778 | patch->sources[0].role, |
| 3779 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3780 | #if LOG_NDEBUG == 0 |
| 3781 | for (size_t i = 0; i < patch->num_sinks; i++) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3782 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, |
| 3783 | patch->sinks[i].role, |
| 3784 | patch->sinks[i].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3785 | } |
| 3786 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3787 | |
| 3788 | if (index >= 0) { |
| 3789 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3790 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", |
| 3791 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 3792 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3793 | return INVALID_OPERATION; |
| 3794 | } |
| 3795 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 3796 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3800 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3801 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3802 | 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] | 3803 | return BAD_VALUE; |
| 3804 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3805 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 3806 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3807 | if (patchDesc != 0) { |
| 3808 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3809 | ALOGV("%s source id differs for patch current id %d new id %d", |
| 3810 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3811 | return BAD_VALUE; |
| 3812 | } |
| 3813 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3814 | DeviceVector devices; |
| 3815 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3816 | // Only support mix to devices connection |
| 3817 | // TODO add support for mix to mix connection |
| 3818 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3819 | ALOGV("%s source mix but sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3820 | return INVALID_OPERATION; |
| 3821 | } |
| 3822 | sp<DeviceDescriptor> devDesc = |
| 3823 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3824 | if (devDesc == 0) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3825 | 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] | 3826 | return BAD_VALUE; |
| 3827 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3828 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3829 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3830 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3831 | NULL, // updatedSamplingRate |
| 3832 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3833 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3834 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3835 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3836 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3837 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3838 | return INVALID_OPERATION; |
| 3839 | } |
| 3840 | devices.add(devDesc); |
| 3841 | } |
| 3842 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3843 | return INVALID_OPERATION; |
| 3844 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3845 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3846 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3847 | ALOGV("%s setting device %s on output %d", |
| 3848 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3849 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3850 | index = mAudioPatches.indexOfKey(*handle); |
| 3851 | if (index >= 0) { |
| 3852 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3853 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3854 | } |
| 3855 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3856 | patchDesc->setUid(uid); |
| 3857 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3858 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3859 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3860 | return INVALID_OPERATION; |
| 3861 | } |
| 3862 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3863 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3864 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3865 | // only one sink supported when connecting an input device to a mix |
| 3866 | if (patch->num_sinks > 1) { |
| 3867 | return INVALID_OPERATION; |
| 3868 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3869 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3870 | if (inputDesc == NULL) { |
| 3871 | return BAD_VALUE; |
| 3872 | } |
| 3873 | if (patchDesc != 0) { |
| 3874 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3875 | return BAD_VALUE; |
| 3876 | } |
| 3877 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3878 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3879 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3880 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3881 | return BAD_VALUE; |
| 3882 | } |
| 3883 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3884 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3885 | patch->sinks[0].sample_rate, |
| 3886 | NULL, /*updatedSampleRate*/ |
| 3887 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3888 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3889 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3890 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3891 | // FIXME for the parameter type, |
| 3892 | // and the NONE |
| 3893 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3894 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3895 | return INVALID_OPERATION; |
| 3896 | } |
| 3897 | // TODO: reconfigure output format and channels here |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3898 | ALOGV("%s setting device %s on output %d", __func__, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3899 | device->toString().c_str(), inputDesc->mIoHandle); |
| 3900 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3901 | index = mAudioPatches.indexOfKey(*handle); |
| 3902 | if (index >= 0) { |
| 3903 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3904 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3905 | } |
| 3906 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3907 | patchDesc->setUid(uid); |
| 3908 | ALOGV("%s success", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3909 | } else { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3910 | ALOGW("%s setInputDevice() failed to create a patch", __func__); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3911 | return INVALID_OPERATION; |
| 3912 | } |
| 3913 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3914 | // device to device connection |
| 3915 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3916 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3917 | return BAD_VALUE; |
| 3918 | } |
| 3919 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3920 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3921 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3922 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3923 | return BAD_VALUE; |
| 3924 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3925 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3926 | //update source and sink with our own data as the data passed in the patch may |
| 3927 | // be incomplete. |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3928 | PatchBuilder patchBuilder; |
| 3929 | audio_port_config sourcePortConfig = {}; |
| 3930 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); |
| 3931 | patchBuilder.addSource(sourcePortConfig); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3932 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3933 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3934 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3935 | ALOGV("%s source device but one sink is not a device", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3936 | return INVALID_OPERATION; |
| 3937 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3938 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3939 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3940 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3941 | return BAD_VALUE; |
| 3942 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3943 | audio_port_config sinkPortConfig = {}; |
| 3944 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); |
| 3945 | patchBuilder.addSink(sinkPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3946 | |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 3947 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for |
| 3948 | // volume management purpose (tracking activity) |
| 3949 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared |
| 3950 | // in config XML to reach the sink so that is can be declared as available. |
| 3951 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3952 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; |
| 3953 | if (sourceDesc != nullptr) { |
| 3954 | // take care of dynamic routing for SwOutput selection, |
| 3955 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 3956 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3957 | audio_attributes_t resultAttr; |
| 3958 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3959 | config.sample_rate = sourceDesc->config().sample_rate; |
| 3960 | config.channel_mask = sourceDesc->config().channel_mask; |
| 3961 | config.format = sourceDesc->config().format; |
| 3962 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 3963 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 3964 | bool isRequestedDeviceForExclusiveUse = false; |
| 3965 | output_type_t outputType; |
| 3966 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, |
| 3967 | &stream, sourceDesc->uid(), &config, &flags, |
| 3968 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, |
| 3969 | nullptr, &outputType); |
| 3970 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3971 | ALOGV("%s no output for device %s", |
| 3972 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 3973 | return INVALID_OPERATION; |
| 3974 | } |
| 3975 | outputDesc = mOutputs.valueFor(output); |
| 3976 | if (outputDesc->isDuplicated()) { |
| 3977 | ALOGE("%s output is duplicated", __func__); |
| 3978 | return INVALID_OPERATION; |
| 3979 | } |
| 3980 | sourceDesc->setSwOutput(outputDesc); |
| 3981 | } |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3982 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3983 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3984 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 3985 | // - audio HAL version is >= 3.0 but no route has been declared between devices |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3986 | // - called from startAudioSource (aka sourceDesc != nullptr) and source device does |
| 3987 | // not have a gain controller |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3988 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 3989 | (srcDevice->getModuleVersionMajor() < 3) || |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3990 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || |
| 3991 | (sourceDesc != nullptr && |
| 3992 | srcDevice->getAudioPort()->getGains().size() == 0)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3993 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3994 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3995 | return INVALID_OPERATION; |
| 3996 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 3997 | if (sourceDesc == nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3998 | SortedVector<audio_io_handle_t> outputs = |
| 3999 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
| 4000 | // if the sink device is reachable via an opened output stream, request to |
| 4001 | // go via this output stream by adding a second source to the patch |
| 4002 | // description |
| 4003 | output = selectOutput(outputs); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4004 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 4005 | outputDesc = mOutputs.valueFor(output); |
| 4006 | if (outputDesc->isDuplicated()) { |
| 4007 | ALOGV("%s output for device %s is duplicated", |
| 4008 | __FUNCTION__, sinkDevice->toString().c_str()); |
| 4009 | return INVALID_OPERATION; |
| 4010 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4011 | } |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4012 | } |
| 4013 | if (outputDesc != nullptr) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4014 | audio_port_config srcMixPortConfig = {}; |
| 4015 | outputDesc->toAudioPortConfig(&srcMixPortConfig, &patch->sources[0]); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4016 | // for volume control, we may need a valid stream |
| 4017 | srcMixPortConfig.ext.mix.usecase.stream = sourceDesc != nullptr ? |
| 4018 | sourceDesc->stream() : AUDIO_STREAM_PATCH; |
| 4019 | patchBuilder.addSource(srcMixPortConfig); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4020 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4021 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4022 | } |
| 4023 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 4024 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4025 | status_t status = installPatch( |
| 4026 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 4027 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4028 | 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] | 4029 | return INVALID_OPERATION; |
| 4030 | } |
| 4031 | } else { |
| 4032 | return BAD_VALUE; |
| 4033 | } |
| 4034 | } else { |
| 4035 | return BAD_VALUE; |
| 4036 | } |
| 4037 | return NO_ERROR; |
| 4038 | } |
| 4039 | |
| 4040 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 4041 | uid_t uid) |
| 4042 | { |
| 4043 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 4044 | |
| 4045 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 4046 | |
| 4047 | if (index < 0) { |
| 4048 | return BAD_VALUE; |
| 4049 | } |
| 4050 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4051 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", |
| 4052 | __func__, mUidCached, patchDesc->getUid(), uid); |
| 4053 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4054 | return INVALID_OPERATION; |
| 4055 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4056 | return releaseAudioPatchInternal(handle); |
| 4057 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4058 | |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4059 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, |
| 4060 | uint32_t delayMs) |
| 4061 | { |
| 4062 | ALOGV("%s patch %d", __func__, handle); |
| 4063 | if (mAudioPatches.indexOfKey(handle) < 0) { |
| 4064 | ALOGE("%s: no patch found with handle=%d", __func__, handle); |
| 4065 | return BAD_VALUE; |
| 4066 | } |
| 4067 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4068 | struct audio_patch *patch = &patchDesc->mPatch; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4069 | patchDesc->setUid(mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4070 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4071 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4072 | if (outputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4073 | 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] | 4074 | return BAD_VALUE; |
| 4075 | } |
| 4076 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4077 | setOutputDevices(outputDesc, |
| 4078 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 4079 | true, |
| 4080 | 0, |
| 4081 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4082 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 4083 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4084 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4085 | if (inputDesc == NULL) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4086 | 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] | 4087 | return BAD_VALUE; |
| 4088 | } |
| 4089 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4090 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4091 | true, |
| 4092 | NULL); |
| 4093 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4094 | status_t status = |
| 4095 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
| 4096 | ALOGV("%s patch panel returned %d patchHandle %d", |
| 4097 | __func__, status, patchDesc->getAfHandle()); |
| 4098 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4099 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4100 | mpClientInterface->onAudioPatchListUpdate(); |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4101 | // SW Bridge |
| 4102 | if (patch->num_sources > 1 && patch->sources[1].type == AUDIO_PORT_TYPE_MIX) { |
| 4103 | sp<SwAudioOutputDescriptor> outputDesc = |
| 4104 | mOutputs.getOutputFromId(patch->sources[1].id); |
| 4105 | if (outputDesc == NULL) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 4106 | ALOGW("%s output not found for id %d", __func__, patch->sources[0].id); |
| 4107 | // releaseOutput has already called closeOuput in case of direct output |
| 4108 | return NO_ERROR; |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4109 | } |
Francois Gaffie | 8e54454 | 2020-05-11 14:12:53 +0200 | [diff] [blame] | 4110 | if (patchDesc->getHandle() != outputDesc->getPatchHandle()) { |
| 4111 | // force SwOutput patch removal as AF counter part patch has already gone. |
| 4112 | ALOGV("%s reset patch handle on Output as different from SWBridge", __func__); |
| 4113 | removeAudioPatch(outputDesc->getPatchHandle()); |
| 4114 | } |
Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4115 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
| 4116 | setOutputDevices(outputDesc, |
| 4117 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 4118 | true, /*force*/ |
| 4119 | 0, |
| 4120 | NULL); |
| 4121 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4122 | } else { |
| 4123 | return BAD_VALUE; |
| 4124 | } |
| 4125 | } else { |
| 4126 | return BAD_VALUE; |
| 4127 | } |
| 4128 | return NO_ERROR; |
| 4129 | } |
| 4130 | |
| 4131 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 4132 | struct audio_patch *patches, |
| 4133 | unsigned int *generation) |
| 4134 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4135 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4136 | return BAD_VALUE; |
| 4137 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4138 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4139 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4140 | } |
| 4141 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4142 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4143 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4144 | ALOGV("setAudioPortConfig()"); |
| 4145 | |
| 4146 | if (config == NULL) { |
| 4147 | return BAD_VALUE; |
| 4148 | } |
| 4149 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 4150 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4151 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 4152 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4153 | } |
| 4154 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4155 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4156 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 4157 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4158 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4159 | if (outputDesc == NULL) { |
| 4160 | return BAD_VALUE; |
| 4161 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4162 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 4163 | "setAudioPortConfig() called on duplicated output %d", |
| 4164 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4165 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4166 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4167 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4168 | if (inputDesc == NULL) { |
| 4169 | return BAD_VALUE; |
| 4170 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4171 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4172 | } else { |
| 4173 | return BAD_VALUE; |
| 4174 | } |
| 4175 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 4176 | sp<DeviceDescriptor> deviceDesc; |
| 4177 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 4178 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 4179 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 4180 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 4181 | } else { |
| 4182 | return BAD_VALUE; |
| 4183 | } |
| 4184 | if (deviceDesc == NULL) { |
| 4185 | return BAD_VALUE; |
| 4186 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4187 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4188 | } else { |
| 4189 | return BAD_VALUE; |
| 4190 | } |
| 4191 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4192 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4193 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 4194 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4195 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4196 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 4197 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4198 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4199 | if (status != NO_ERROR) { |
| 4200 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4201 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4202 | |
| 4203 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4204 | } |
| 4205 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4206 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 4207 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4208 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4209 | clearAudioPatches(uid); |
| 4210 | clearSessionRoutes(uid); |
| 4211 | } |
| 4212 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4213 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 4214 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4215 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4216 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4217 | if (patchDesc->getUid() == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4218 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4219 | } |
| 4220 | } |
| 4221 | } |
| 4222 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4223 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4224 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4225 | // Take the first attributes following the product strategy as it is used to retrieve the routed |
| 4226 | // device. All attributes wihin a strategy follows the same "routing strategy" |
| 4227 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); |
| 4228 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4229 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4230 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 4231 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 4232 | continue; |
| 4233 | } |
| 4234 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4235 | if (!outputDesc->isStrategyActive(ps)) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4236 | continue; |
| 4237 | } |
| 4238 | // If the default device for this strategy is on another output mix, |
| 4239 | // invalidate all tracks in this strategy to force re connection. |
| 4240 | // Otherwise select new device on the output mix. |
| 4241 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4242 | for (auto stream : mEngine->getStreamTypesForProductStrategy(ps)) { |
| 4243 | mpClientInterface->invalidateStream(stream); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4244 | } |
| 4245 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4246 | setOutputDevices( |
| 4247 | outputDesc, getNewOutputDevices(outputDesc, false /*fromCache*/), false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4248 | } |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 4253 | { |
| 4254 | // remove output routes associated with this uid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4255 | std::vector<product_strategy_t> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4256 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4257 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4258 | for (const auto& client : outputDesc->getClientIterable()) { |
| 4259 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 4260 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4261 | auto clientStrategy = client->strategy(); |
| 4262 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != |
| 4263 | end(affectedStrategies)) { |
| 4264 | continue; |
| 4265 | } |
| 4266 | affectedStrategies.push_back(client->strategy()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4267 | } |
| 4268 | } |
| 4269 | } |
| 4270 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4271 | for (const auto& strategy : affectedStrategies) { |
| 4272 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | // remove input routes associated with this uid |
| 4276 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4277 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4278 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4279 | for (const auto& client : inputDesc->getClientIterable()) { |
| 4280 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 4281 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 4282 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4283 | } |
| 4284 | } |
| 4285 | } |
| 4286 | // reroute inputs if necessary |
| 4287 | SortedVector<audio_io_handle_t> inputsToClose; |
| 4288 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4289 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 4290 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4291 | inputsToClose.add(inputDesc->mIoHandle); |
| 4292 | } |
| 4293 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4294 | for (const auto& input : inputsToClose) { |
| 4295 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4296 | } |
| 4297 | } |
| 4298 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4299 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 4300 | { |
| 4301 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4302 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 4303 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4304 | stopAudioSource(mAudioSources.keyAt(i)); |
| 4305 | } |
| 4306 | } |
| 4307 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4308 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4309 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 4310 | audio_io_handle_t *ioHandle, |
| 4311 | audio_devices_t *device) |
| 4312 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 4313 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 4314 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4315 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4316 | *device = mEngine->getInputDeviceForAttributes(attr)->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4317 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 4318 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4321 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4322 | const audio_attributes_t *attributes, |
| 4323 | audio_port_handle_t *portId, |
| 4324 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4325 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4326 | ALOGV("%s", __FUNCTION__); |
| 4327 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 4328 | |
| 4329 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 4330 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 4331 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4332 | return BAD_VALUE; |
| 4333 | } |
| 4334 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4335 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 4336 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4337 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 4338 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4339 | return INVALID_OPERATION; |
| 4340 | } |
| 4341 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4342 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4343 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4344 | String8(source->ext.device.address), |
| 4345 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4346 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4347 | 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] | 4348 | return BAD_VALUE; |
| 4349 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4350 | |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 4351 | *portId = PolicyAudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4352 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4353 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4354 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4355 | mEngine->getStreamTypeForAttributes(*attributes), |
| 4356 | mEngine->getProductStrategyForAttributes(*attributes), |
| 4357 | toVolumeSource(*attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4358 | |
| 4359 | status_t status = connectAudioSource(sourceDesc); |
| 4360 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4361 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4362 | } |
| 4363 | return status; |
| 4364 | } |
| 4365 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4366 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4367 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4368 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4369 | |
| 4370 | // make sure we only have one patch per source. |
| 4371 | disconnectAudioSource(sourceDesc); |
| 4372 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4373 | audio_attributes_t attributes = sourceDesc->attributes(); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4374 | // May the device (dynamic) have been disconnected/reconnected, id has changed. |
| 4375 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( |
| 4376 | sourceDesc->srcDevice()->type(), |
| 4377 | String8(sourceDesc->srcDevice()->address().c_str()), |
| 4378 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4379 | DeviceVector sinkDevices = |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 4380 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4381 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4382 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4383 | if (!mAvailableOutputDevices.contains(sinkDevice)) { |
| 4384 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); |
| 4385 | return INVALID_OPERATION; |
| 4386 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4387 | PatchBuilder patchBuilder; |
| 4388 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); |
| 4389 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; |
| 4390 | status_t status = |
| 4391 | createAudioPatchInternal(patchBuilder.patch(), &handle, mUidCached, 0, sourceDesc); |
| 4392 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { |
| 4393 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); |
| 4394 | return INVALID_OPERATION; |
| 4395 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4396 | sourceDesc->connect(handle, sinkDevice); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4397 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") |
| 4398 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4399 | if (swOutput != 0) { |
| 4400 | status = swOutput->start(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4401 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4402 | goto FailureSourceAdded; |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4403 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4404 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 4405 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4406 | goto FailureReleasePatch; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 4407 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4408 | swOutput->addClient(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4409 | uint32_t delayMs = 0; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4410 | status = startSource(swOutput, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4411 | if (status != NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4412 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); |
| 4413 | goto FailureSourceActive; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4414 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4415 | if (delayMs != 0) { |
| 4416 | usleep(delayMs * 1000); |
| 4417 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4418 | } else { |
| 4419 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
| 4420 | if (hwOutputDesc != 0) { |
| 4421 | // create Hwoutput and add to mHwOutputs |
| 4422 | } else { |
| 4423 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 4424 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4425 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4426 | return NO_ERROR; |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4427 | |
| 4428 | FailureSourceActive: |
| 4429 | swOutput->stop(); |
| 4430 | releaseOutput(sourceDesc->portId()); |
| 4431 | FailureSourceAdded: |
| 4432 | sourceDesc->setSwOutput(nullptr); |
| 4433 | FailureReleasePatch: |
| 4434 | releaseAudioPatchInternal(handle); |
| 4435 | return INVALID_OPERATION; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4436 | } |
| 4437 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4438 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4439 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4440 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 4441 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4442 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4443 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4444 | return BAD_VALUE; |
| 4445 | } |
| 4446 | status_t status = disconnectAudioSource(sourceDesc); |
| 4447 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4448 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4449 | return status; |
| 4450 | } |
| 4451 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4452 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 4453 | { |
| 4454 | if (mMasterMono == mono) { |
| 4455 | return NO_ERROR; |
| 4456 | } |
| 4457 | mMasterMono = mono; |
| 4458 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 4459 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 4460 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 4461 | // |
| 4462 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 4463 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 4464 | // play back offloaded. |
| 4465 | if (mMasterMono) { |
| 4466 | Vector<audio_io_handle_t> offloaded; |
| 4467 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 4468 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4469 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 4470 | offloaded.push(desc->mIoHandle); |
| 4471 | } |
| 4472 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4473 | for (const auto& handle : offloaded) { |
| 4474 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4475 | } |
| 4476 | } |
| 4477 | // update master mono for all remaining outputs |
| 4478 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 4479 | updateMono(mOutputs.keyAt(i)); |
| 4480 | } |
| 4481 | return NO_ERROR; |
| 4482 | } |
| 4483 | |
| 4484 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 4485 | { |
| 4486 | *mono = mMasterMono; |
| 4487 | return NO_ERROR; |
| 4488 | } |
| 4489 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 4490 | float AudioPolicyManager::getStreamVolumeDB( |
| 4491 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 4492 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 4493 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 4494 | } |
| 4495 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4496 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 4497 | audio_format_t *surroundFormats, |
| 4498 | bool *surroundFormatsEnabled, |
| 4499 | bool reported) |
| 4500 | { |
| 4501 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 4502 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 4503 | return BAD_VALUE; |
| 4504 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4505 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p reported %d", |
| 4506 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4507 | |
| 4508 | size_t formatsWritten = 0; |
| 4509 | size_t formatsMax = *numSurroundFormats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4510 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4511 | if (reported) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4512 | // Return formats from all device profiles that have already been resolved by |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 4513 | // checkOutputsForDevice(). |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4514 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 4515 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 4516 | audio_devices_t deviceType = device->type(); |
| 4517 | // Enabling/disabling formats are applied to only HDMI devices. So, this function |
| 4518 | // returns formats reported by HDMI devices. |
| 4519 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { |
| 4520 | continue; |
| 4521 | } |
| 4522 | // Formats reported by sink devices |
| 4523 | std::unordered_set<audio_format_t> formatset; |
| 4524 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { |
| 4525 | formatset.insert(it->second.begin(), it->second.end()); |
| 4526 | } |
| 4527 | |
| 4528 | // Formats hard-coded in the in policy configuration file (if any). |
| 4529 | FormatVector encodedFormats = device->encodedFormats(); |
| 4530 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); |
| 4531 | // Filter the formats which are supported by the vendor hardware. |
| 4532 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { |
| 4533 | if (mConfig.getSurroundFormats().count(*it) != 0) { |
| 4534 | formats.insert(*it); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4535 | } else { |
| 4536 | for (const auto& pair : mConfig.getSurroundFormats()) { |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 4537 | if (pair.second.count(*it) != 0) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4538 | formats.insert(pair.first); |
| 4539 | break; |
| 4540 | } |
| 4541 | } |
| 4542 | } |
| 4543 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4544 | } |
| 4545 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 4546 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 4547 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4548 | } |
| 4549 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4550 | *numSurroundFormats = formats.size(); |
| 4551 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 4552 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 4553 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4554 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 4555 | surroundFormats[formatsWritten] = format; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4556 | bool formatEnabled = true; |
| 4557 | switch (forceUse) { |
| 4558 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
| 4559 | formatEnabled = mManualSurroundFormats.count(format) != 0; |
| 4560 | break; |
| 4561 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 4562 | formatEnabled = false; |
| 4563 | break; |
| 4564 | default: // AUTO or ALWAYS => true |
| 4565 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4566 | } |
| 4567 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 4568 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4569 | } |
| 4570 | return NO_ERROR; |
| 4571 | } |
| 4572 | |
| 4573 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 4574 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4575 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 4576 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 4577 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4578 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4579 | return BAD_VALUE; |
| 4580 | } |
| 4581 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4582 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 4583 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4584 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4585 | return INVALID_OPERATION; |
| 4586 | } |
| 4587 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4588 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4589 | return NO_ERROR; |
| 4590 | } |
| 4591 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4592 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4593 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4594 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 4595 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4596 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4597 | } |
| 4598 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4599 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 4600 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4601 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4602 | } |
| 4603 | } |
| 4604 | |
| 4605 | sp<SwAudioOutputDescriptor> outputDesc; |
| 4606 | bool profileUpdated = false; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 4607 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( |
| 4608 | AUDIO_DEVICE_OUT_HDMI); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4609 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 4610 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4611 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4612 | std::string name = hdmiOutputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4613 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 4614 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 4615 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4616 | name.c_str(), |
| 4617 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4618 | if (status != NO_ERROR) { |
| 4619 | continue; |
| 4620 | } |
| 4621 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 4622 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 4623 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4624 | name.c_str(), |
| 4625 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4626 | profileUpdated |= (status == NO_ERROR); |
| 4627 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4628 | // 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] | 4629 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4630 | AUDIO_DEVICE_IN_HDMI); |
| 4631 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 4632 | // Simulate reconnection to update enabled surround sound formats. |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4633 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4634 | std::string name = hdmiInputDevices[i]->getName(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4635 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 4636 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 4637 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4638 | name.c_str(), |
| 4639 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4640 | if (status != NO_ERROR) { |
| 4641 | continue; |
| 4642 | } |
| 4643 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 4644 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 4645 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4646 | name.c_str(), |
| 4647 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4648 | profileUpdated |= (status == NO_ERROR); |
| 4649 | } |
| 4650 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4651 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4652 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4653 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 4657 | } |
| 4658 | |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 4659 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 4660 | { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 4661 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 4662 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 4663 | mInputs.valueAt(i)->setAppState(portId, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 4664 | } |
| 4665 | } |
| 4666 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 4667 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 4668 | { |
| 4669 | for (const auto& hwModule : mHwModules) { |
| 4670 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 4671 | for (const auto &outProfile : outputProfiles) { |
| 4672 | struct audio_port audioPort; |
| 4673 | outProfile->toAudioPort(&audioPort); |
| 4674 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 4675 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 4676 | return true; |
| 4677 | } |
| 4678 | } |
| 4679 | } |
| 4680 | } |
| 4681 | return false; |
| 4682 | } |
| 4683 | |
Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 4684 | bool AudioPolicyManager::isCallScreenModeSupported() |
| 4685 | { |
| 4686 | return getConfig().isCallScreenModeSupported(); |
| 4687 | } |
| 4688 | |
| 4689 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4690 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4691 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4692 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4693 | if (!sourceDesc->isConnected()) { |
| 4694 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); |
| 4695 | return NO_ERROR; |
| 4696 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4697 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); |
| 4698 | if (swOutput != 0) { |
| 4699 | status_t status = stopSource(swOutput, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4700 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4701 | swOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4702 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 4703 | if (releaseOutput(sourceDesc->portId())) { |
| 4704 | // The output descriptor is reopened to query dynamic profiles. In that case, there is |
| 4705 | // no need to release audio patch here but just return NO_ERROR. |
| 4706 | return NO_ERROR; |
| 4707 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4708 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4709 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4710 | if (hwOutputDesc != 0) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4711 | // close Hwoutput and remove from mHwOutputs |
| 4712 | } else { |
| 4713 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 4714 | } |
| 4715 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4716 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle()); |
| 4717 | sourceDesc->disconnect(); |
| 4718 | return status; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4721 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( |
| 4722 | audio_io_handle_t output, const audio_attributes_t &attr) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4723 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4724 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4725 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4726 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4727 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4728 | if (followsSameRouting(attr, sourceDesc->attributes()) && |
| 4729 | outputDesc != 0 && outputDesc->mIoHandle == output) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4730 | source = sourceDesc; |
| 4731 | break; |
| 4732 | } |
| 4733 | } |
| 4734 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4735 | } |
| 4736 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4737 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4738 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4739 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4740 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 4741 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 4742 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4743 | } |
| 4744 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 4745 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 4746 | if (std::string audioPolicyXmlConfigFile = audio_get_audio_policy_config_file(); |
| 4747 | !audioPolicyXmlConfigFile.empty()) { |
| 4748 | status_t ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile.c_str(), &config); |
| 4749 | if (ret == NO_ERROR) { |
| 4750 | config.setSource(audioPolicyXmlConfigFile); |
Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 4751 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 4752 | return ret; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 4753 | } |
Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 4754 | return BAD_VALUE; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 4755 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 4756 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4757 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 4758 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4759 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 4760 | 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] | 4761 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4762 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4763 | mA2dpSuspended(false), |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4764 | mConfig(mHwModulesAll, mOutputDevicesAll, mInputDevicesAll, mDefaultOutputDevice), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4765 | mAudioPortGeneration(1), |
| 4766 | mBeaconMuteRefCount(0), |
| 4767 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4768 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4769 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4770 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 4771 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4772 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4773 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4774 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4775 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 4776 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 4777 | { |
| 4778 | loadConfig(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4779 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4780 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 4781 | void AudioPolicyManager::loadConfig() { |
| 4782 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4783 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4784 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4785 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | status_t AudioPolicyManager::initialize() { |
Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 4789 | { |
| 4790 | auto engLib = EngineLibrary::load( |
| 4791 | "libaudiopolicyengine" + getConfig().getEngineLibraryNameSuffix() + ".so"); |
| 4792 | if (!engLib) { |
| 4793 | ALOGE("%s: Failed to load the engine library", __FUNCTION__); |
| 4794 | return NO_INIT; |
| 4795 | } |
| 4796 | mEngine = engLib->createEngine(); |
| 4797 | if (mEngine == nullptr) { |
| 4798 | ALOGE("%s: Failed to instantiate the APM engine", __FUNCTION__); |
| 4799 | return NO_INIT; |
| 4800 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4801 | } |
| 4802 | mEngine->setObserver(this); |
| 4803 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4804 | if (status != NO_ERROR) { |
| 4805 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 4806 | return status; |
| 4807 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4808 | |
Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 4809 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( |
| 4810 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); |
| 4811 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4812 | // after parsing the config, mOutputDevicesAll and mInputDevicesAll contain all known devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4813 | // open all output streams needed to access attached devices |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 4814 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4815 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4816 | // make sure default device is reachable |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4817 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { |
| 4818 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", |
| 4819 | mDefaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4820 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4821 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4822 | // If microphones address is empty, set it according to device type |
Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 4823 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4824 | if (mAvailableInputDevices[i]->address().empty()) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4825 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4826 | mAvailableInputDevices[i]->setAddress(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4827 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4828 | mAvailableInputDevices[i]->setAddress(AUDIO_BACK_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4829 | } |
| 4830 | } |
| 4831 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4832 | |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 4833 | 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] | 4834 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4835 | // Silence ALOGV statements |
| 4836 | property_set("log.tag." LOG_TAG, "D"); |
| 4837 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4838 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4839 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4840 | } |
| 4841 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4842 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4843 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4844 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4845 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4846 | } |
| 4847 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4848 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4849 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4850 | mAvailableOutputDevices.clear(); |
| 4851 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4852 | mOutputs.clear(); |
| 4853 | mInputs.clear(); |
| 4854 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4855 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4856 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4857 | } |
| 4858 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4859 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4860 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4861 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 | } |
| 4863 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4864 | // --- |
| 4865 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4866 | void AudioPolicyManager::onNewAudioModulesAvailable() |
| 4867 | { |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 4868 | DeviceVector newDevices; |
| 4869 | onNewAudioModulesAvailableInt(&newDevices); |
| 4870 | if (!newDevices.empty()) { |
| 4871 | nextAudioPortGeneration(); |
| 4872 | mpClientInterface->onAudioPortListUpdate(); |
| 4873 | } |
| 4874 | } |
| 4875 | |
| 4876 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) |
| 4877 | { |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4878 | for (const auto& hwModule : mHwModulesAll) { |
| 4879 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { |
| 4880 | continue; |
| 4881 | } |
| 4882 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
| 4883 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 4884 | ALOGW("could not open HW module %s", hwModule->getName()); |
| 4885 | continue; |
| 4886 | } |
| 4887 | mHwModules.push_back(hwModule); |
| 4888 | // open all output streams needed to access attached devices |
| 4889 | // except for direct output streams that are only opened when they are actually |
| 4890 | // required by an app. |
| 4891 | // This also validates mAvailableOutputDevices list |
| 4892 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
| 4893 | if (!outProfile->canOpenNewIo()) { |
| 4894 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 4895 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 4896 | continue; |
| 4897 | } |
| 4898 | if (!outProfile->hasSupportedDevices()) { |
| 4899 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
| 4900 | continue; |
| 4901 | } |
| 4902 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
| 4903 | mTtsOutputAvailable = true; |
| 4904 | } |
| 4905 | |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4906 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 4907 | DeviceVector availProfileDevices = supportedDevices.filter(mOutputDevicesAll); |
| 4908 | sp<DeviceDescriptor> supportedDevice = 0; |
| 4909 | if (supportedDevices.contains(mDefaultOutputDevice)) { |
| 4910 | supportedDevice = mDefaultOutputDevice; |
| 4911 | } else { |
| 4912 | // choose first device present in profile's SupportedDevices also part of |
| 4913 | // mAvailableOutputDevices. |
| 4914 | if (availProfileDevices.isEmpty()) { |
| 4915 | continue; |
| 4916 | } |
| 4917 | supportedDevice = availProfileDevices.itemAt(0); |
| 4918 | } |
| 4919 | if (!mOutputDevicesAll.contains(supportedDevice)) { |
| 4920 | continue; |
| 4921 | } |
| 4922 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 4923 | mpClientInterface); |
| 4924 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 4925 | status_t status = outputDesc->open(nullptr, DeviceVector(supportedDevice), |
| 4926 | AUDIO_STREAM_DEFAULT, |
| 4927 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4928 | if (status != NO_ERROR) { |
| 4929 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 4930 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 4931 | continue; |
| 4932 | } |
| 4933 | for (const auto &device : availProfileDevices) { |
| 4934 | // give a valid ID to an attached device once confirmed it is reachable |
| 4935 | if (!device->isAttached()) { |
| 4936 | device->attach(hwModule); |
| 4937 | mAvailableOutputDevices.add(device); |
jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 4938 | device->setEncapsulationInfoFromHal(mpClientInterface); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 4939 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4940 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 4941 | } |
| 4942 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 4943 | if (mPrimaryOutput == nullptr && |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4944 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 4945 | mPrimaryOutput = outputDesc; |
| 4946 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 4947 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
| 4948 | outputDesc->close(); |
| 4949 | } else { |
| 4950 | addOutput(output, outputDesc); |
| 4951 | setOutputDevices(outputDesc, |
| 4952 | DeviceVector(supportedDevice), |
| 4953 | true, |
| 4954 | 0, |
| 4955 | NULL); |
| 4956 | } |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4957 | } |
| 4958 | // open input streams needed to access attached devices to validate |
| 4959 | // mAvailableInputDevices list |
| 4960 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
| 4961 | if (!inProfile->canOpenNewIo()) { |
| 4962 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4963 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4964 | continue; |
| 4965 | } |
| 4966 | if (!inProfile->hasSupportedDevices()) { |
| 4967 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
| 4968 | continue; |
| 4969 | } |
| 4970 | // chose first device present in profile's SupportedDevices also part of |
| 4971 | // available input devices |
| 4972 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 4973 | DeviceVector availProfileDevices = supportedDevices.filter(mInputDevicesAll); |
| 4974 | if (availProfileDevices.isEmpty()) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 4975 | ALOGV("%s: Input device list is empty! for profile %s", |
| 4976 | __func__, inProfile->getTagName().c_str()); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4977 | continue; |
| 4978 | } |
| 4979 | sp<AudioInputDescriptor> inputDesc = |
| 4980 | new AudioInputDescriptor(inProfile, mpClientInterface); |
| 4981 | |
| 4982 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 4983 | status_t status = inputDesc->open(nullptr, |
| 4984 | availProfileDevices.itemAt(0), |
| 4985 | AUDIO_SOURCE_MIC, |
| 4986 | AUDIO_INPUT_FLAG_NONE, |
| 4987 | &input); |
| 4988 | if (status != NO_ERROR) { |
| 4989 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 4990 | availProfileDevices.toString().c_str(), |
| 4991 | hwModule->getName()); |
| 4992 | continue; |
| 4993 | } |
| 4994 | for (const auto &device : availProfileDevices) { |
| 4995 | // give a valid ID to an attached device once confirmed it is reachable |
| 4996 | if (!device->isAttached()) { |
| 4997 | device->attach(hwModule); |
| 4998 | device->importAudioPortAndPickAudioProfile(inProfile, true); |
| 4999 | mAvailableInputDevices.add(device); |
Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5000 | if (newDevices) newDevices->add(device); |
Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5001 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 5002 | } |
| 5003 | } |
| 5004 | inputDesc->close(); |
| 5005 | } |
| 5006 | } |
| 5007 | } |
| 5008 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5009 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 5010 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5011 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5012 | mOutputs.add(output, outputDesc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5013 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5014 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5015 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5016 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5017 | } |
| 5018 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5019 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 5020 | { |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5021 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { |
| 5022 | ALOGV("%s: removing primary output", __func__); |
| 5023 | mPrimaryOutput = nullptr; |
| 5024 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5025 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5026 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5027 | } |
| 5028 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5029 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 5030 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5031 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5032 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5033 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5034 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5035 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5036 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5037 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5038 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5039 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5040 | audio_devices_t deviceType = device->type(); |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5041 | const String8 &address = String8(device->address().c_str()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5042 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5043 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5044 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5045 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5046 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5047 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5048 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5049 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 5050 | // first call getAudioPort to get the supported attributes from the HAL |
| 5051 | struct audio_port_v7 port = {}; |
| 5052 | device->toAudioPort(&port); |
| 5053 | status_t status = mpClientInterface->getAudioPort(&port); |
| 5054 | if (status == NO_ERROR) { |
| 5055 | device->importAudioPort(port); |
| 5056 | } |
| 5057 | |
| 5058 | // then list already open outputs that can be routed to this device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5059 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5060 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5061 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5062 | && desc->devicesSupportEncodedFormats({deviceType})) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5063 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 5064 | mOutputs.keyAt(i), device->toString().c_str()); |
| 5065 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5066 | } |
| 5067 | } |
| 5068 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5069 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5070 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5071 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 5072 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5073 | if (profile->supportsDevice(device)) { |
| 5074 | profiles.add(profile); |
| 5075 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 5076 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5077 | } |
| 5078 | } |
| 5079 | } |
| 5080 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 5081 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5082 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5083 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5084 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5085 | return BAD_VALUE; |
| 5086 | } |
| 5087 | |
| 5088 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 5089 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 5090 | 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] | 5091 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5092 | |
| 5093 | // nothing to do if one output is already opened for this profile |
| 5094 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5095 | for (j = 0; j < outputs.size(); j++) { |
| 5096 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5097 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5098 | // matching profile: save the sample rates, format and channel masks supported |
| 5099 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5100 | if (audio_device_is_digital(deviceType)) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5101 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5102 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5103 | break; |
| 5104 | } |
| 5105 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5106 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5107 | continue; |
| 5108 | } |
| 5109 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5110 | if (!profile->canOpenNewIo()) { |
| 5111 | ALOGW("Max Output number %u already opened for this profile %s", |
| 5112 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 5113 | continue; |
| 5114 | } |
| 5115 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 5116 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5117 | deviceType, address.string(), profile.get(), profile->getName().c_str()); |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5118 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); |
| 5119 | 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] | 5120 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5121 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5122 | profiles.removeAt(profile_index); |
| 5123 | profile_index--; |
| 5124 | } else { |
| 5125 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5126 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5127 | if (audio_device_is_digital(deviceType)) { |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5128 | // TODO: when getAudioPort is ready, it may not be needed to import the audio |
| 5129 | // port but just pick audio profile |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5130 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5131 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5132 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5133 | if (device_distinguishes_on_address(deviceType)) { |
| 5134 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 5135 | device->toString().c_str()); |
| 5136 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 5137 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5138 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5139 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 5140 | } |
| 5141 | } |
| 5142 | |
| 5143 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5144 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5145 | return BAD_VALUE; |
| 5146 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5147 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5148 | // check if one opened output is not needed any more after disconnecting one device |
| 5149 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5150 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5151 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5152 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5153 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5154 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5155 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5156 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5157 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 5158 | mOutputs.keyAt(i)); |
| 5159 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5160 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5161 | } |
| 5162 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5163 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5164 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5165 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 5166 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5167 | if (!profile->supportsDevice(device)) { |
| 5168 | continue; |
| 5169 | } |
| 5170 | ALOGV("checkOutputsForDevice(): " |
| 5171 | "clearing direct output profile %zu on module %s", |
| 5172 | j, hwModule->getName()); |
| 5173 | profile->clearAudioProfiles(); |
| 5174 | if (!profile->hasDynamicAudioProfile()) { |
| 5175 | continue; |
| 5176 | } |
| 5177 | // When a device is disconnected, if there is an IOProfile that contains dynamic |
| 5178 | // profiles and supports the disconnected device, call getAudioPort to repopulate |
| 5179 | // the capabilities of the devices that is supported by the IOProfile. |
| 5180 | for (const auto& supportedDevice : profile->getSupportedDevices()) { |
| 5181 | if (supportedDevice == device || |
| 5182 | !mAvailableOutputDevices.contains(supportedDevice)) { |
| 5183 | continue; |
| 5184 | } |
| 5185 | struct audio_port_v7 port; |
| 5186 | supportedDevice->toAudioPort(&port); |
| 5187 | status_t status = mpClientInterface->getAudioPort(&port); |
| 5188 | if (status == NO_ERROR) { |
| 5189 | supportedDevice->importAudioPort(port); |
| 5190 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5191 | } |
| 5192 | } |
| 5193 | } |
| 5194 | } |
| 5195 | return NO_ERROR; |
| 5196 | } |
| 5197 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5198 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5199 | audio_policy_dev_state_t state) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5200 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5201 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5202 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5203 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5204 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5205 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5208 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5209 | // look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5210 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5211 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5212 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5213 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5214 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5215 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5216 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5217 | if (profile->supportsDevice(device)) { |
| 5218 | profiles.add(profile); |
| 5219 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 5220 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5221 | } |
| 5222 | } |
| 5223 | } |
| 5224 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5225 | if (profiles.isEmpty()) { |
| 5226 | ALOGW("%s: No input profile available for device %s", |
| 5227 | __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5228 | return BAD_VALUE; |
| 5229 | } |
| 5230 | |
| 5231 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 5232 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 5233 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 5234 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5235 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5236 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5237 | // nothing to do if one input is already opened for this profile |
| 5238 | size_t input_index; |
| 5239 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 5240 | desc = mInputs.valueAt(input_index); |
| 5241 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5242 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5243 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5244 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5245 | break; |
| 5246 | } |
| 5247 | } |
| 5248 | if (input_index != mInputs.size()) { |
| 5249 | continue; |
| 5250 | } |
| 5251 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5252 | if (!profile->canOpenNewIo()) { |
| 5253 | ALOGW("Max Input number %u already opened for this profile %s", |
| 5254 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 5255 | continue; |
| 5256 | } |
| 5257 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5258 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5259 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5260 | status_t status = desc->open(nullptr, |
| 5261 | device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5262 | AUDIO_SOURCE_MIC, |
| 5263 | AUDIO_INPUT_FLAG_NONE, |
| 5264 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5265 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5266 | if (status == NO_ERROR) { |
jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5267 | const String8& address = String8(device->address().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5268 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5269 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5270 | mpClientInterface->setParameters(input, String8(param)); |
| 5271 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5272 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5273 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5274 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5275 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5276 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5277 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5278 | } |
| 5279 | |
Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5280 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5281 | addInput(input, desc); |
| 5282 | } |
| 5283 | } // endif input != 0 |
| 5284 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5285 | if (input == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5286 | ALOGW("%s could not open input for device %s", __func__, |
| 5287 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5288 | profiles.removeAt(profile_index); |
| 5289 | profile_index--; |
| 5290 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5291 | if (audio_device_is_digital(device->type())) { |
jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5292 | device->importAudioPortAndPickAudioProfile(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5293 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5294 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 5295 | } |
| 5296 | } // end scan profiles |
| 5297 | |
| 5298 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5299 | 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] | 5300 | return BAD_VALUE; |
| 5301 | } |
| 5302 | } else { |
| 5303 | // Disconnect |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5304 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 5305 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5306 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5307 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5308 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5309 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5310 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 5311 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 5312 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5313 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5314 | } |
| 5315 | } |
| 5316 | } |
| 5317 | } // end disconnect |
| 5318 | |
| 5319 | return NO_ERROR; |
| 5320 | } |
| 5321 | |
| 5322 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5323 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5324 | { |
| 5325 | ALOGV("closeOutput(%d)", output); |
| 5326 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5327 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); |
| 5328 | if (closingOutput == NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5329 | ALOGW("closeOutput() unknown output %d", output); |
| 5330 | return; |
| 5331 | } |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 5332 | const bool closingOutputWasActive = closingOutput->isActive(); |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5333 | mPolicyMixes.closeOutput(closingOutput); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5334 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5335 | // look for duplicated outputs connected to the output being removed. |
| 5336 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5337 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); |
| 5338 | if (dupOutput->isDuplicated() && |
| 5339 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { |
| 5340 | sp<SwAudioOutputDescriptor> remainingOutput = |
| 5341 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5342 | // As all active tracks on duplicated output will be deleted, |
| 5343 | // and as they were also referenced on the other output, the reference |
| 5344 | // count for their stream type must be adjusted accordingly on |
| 5345 | // the other output. |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5346 | const bool wasActive = remainingOutput->isActive(); |
| 5347 | // Note: no-op on the closing output where all clients has already been set inactive |
| 5348 | dupOutput->setAllClientsInactive(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5349 | // stop() will be a no op if the output is still active but is needed in case all |
| 5350 | // active streams refcounts where cleared above |
| 5351 | if (wasActive) { |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5352 | remainingOutput->stop(); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5353 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5354 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 5355 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 5356 | |
| 5357 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5358 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5359 | } |
| 5360 | } |
| 5361 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5362 | nextAudioPortGeneration(); |
| 5363 | |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5364 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5365 | if (index >= 0) { |
| 5366 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5367 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 5368 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5369 | mAudioPatches.removeItemsAt(index); |
| 5370 | mpClientInterface->onAudioPatchListUpdate(); |
| 5371 | } |
| 5372 | |
Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 5373 | if (closingOutputWasActive) { |
| 5374 | closingOutput->stop(); |
| 5375 | } |
François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5376 | closingOutput->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5377 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5378 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5379 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 5380 | |
| 5381 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 5382 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5383 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 5384 | bool directOutputOpen = false; |
| 5385 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5386 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 5387 | directOutputOpen = true; |
| 5388 | break; |
| 5389 | } |
| 5390 | } |
| 5391 | if (!directOutputOpen) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 5392 | ALOGV("no direct outputs open, reset MSD patches"); |
| 5393 | // TODO: The MSD patches to be established here may differ to current MSD patches due to |
| 5394 | // how output devices for patching are resolved. Avoid by caching and reusing the |
| 5395 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output |
| 5396 | // devices to patch to. This may be complicated by the fact that devices may become |
| 5397 | // unavailable. |
| 5398 | setMsdPatches(); |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 5399 | } |
| 5400 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 5404 | { |
| 5405 | ALOGV("closeInput(%d)", input); |
| 5406 | |
| 5407 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 5408 | if (inputDesc == NULL) { |
| 5409 | ALOGW("closeInput() unknown input %d", input); |
| 5410 | return; |
| 5411 | } |
| 5412 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5413 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5414 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5415 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5416 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5417 | if (index >= 0) { |
| 5418 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5419 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( |
| 5420 | patchDesc->getAfHandle(), 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5421 | mAudioPatches.removeItemsAt(index); |
| 5422 | mpClientInterface->onAudioPatchListUpdate(); |
| 5423 | } |
| 5424 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5425 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5426 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 5427 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5428 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 5429 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 5430 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 5431 | mpClientInterface->setSoundTriggerCaptureState(false); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 5432 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5433 | } |
| 5434 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5435 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 5436 | const DeviceVector &devices, |
| 5437 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5438 | { |
| 5439 | SortedVector<audio_io_handle_t> outputs; |
| 5440 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5441 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5442 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5443 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5444 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5445 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5446 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5447 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5448 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5449 | outputs.add(openOutputs.keyAt(i)); |
| 5450 | } |
| 5451 | } |
| 5452 | return outputs; |
| 5453 | } |
| 5454 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 5455 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 5456 | { |
| 5457 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 5458 | // output is suspended before any tracks are moved to it |
| 5459 | checkA2dpSuspend(); |
| 5460 | checkOutputForAllStrategies(); |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5461 | checkSecondaryOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 5462 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 5463 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 5464 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 5465 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how |
| 5466 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device |
| 5467 | // configuration changes will ultimately be rerouted correctly. We can still avoid |
| 5468 | // unnecessary rerouting by caching and reusing the arguments to |
| 5469 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. |
| 5470 | // This may be complicated by the fact that devices may become unavailable. |
| 5471 | setMsdPatches(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 5472 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 5473 | // an event that changed routing likely occurred, inform upper layers |
| 5474 | mpClientInterface->onRoutingUpdated(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 5475 | } |
| 5476 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5477 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, |
| 5478 | const audio_attributes_t &rAttr) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5479 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5480 | return mEngine->getProductStrategyForAttributes(lAttr) == |
| 5481 | mEngine->getProductStrategyForAttributes(rAttr); |
| 5482 | } |
| 5483 | |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5484 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) |
| 5485 | { |
| 5486 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 5487 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5488 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5489 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE |
| 5490 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5491 | connectAudioSource(sourceDesc); |
| 5492 | } |
| 5493 | } |
| 5494 | } |
| 5495 | |
| 5496 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) |
| 5497 | { |
| 5498 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 5499 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5500 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr |
| 5501 | && sourceDesc->swOutput().promote()->mIoHandle == output) { |
| 5502 | disconnectAudioSource(sourceDesc); |
| 5503 | } |
| 5504 | } |
| 5505 | } |
| 5506 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5507 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) |
| 5508 | { |
| 5509 | auto psId = mEngine->getProductStrategyForAttributes(attr); |
| 5510 | |
| 5511 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); |
| 5512 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); |
Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 5513 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5514 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 5515 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5516 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5517 | uint32_t maxLatency = 0; |
| 5518 | bool invalidate = false; |
| 5519 | // take into account dynamic audio policies related changes: if a client is now associated |
| 5520 | // to a different policy mix than at creation time, invalidate corresponding stream |
| 5521 | for (size_t i = 0; i < mPreviousOutputs.size() && !invalidate; i++) { |
| 5522 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); |
| 5523 | if (desc->isDuplicated()) { |
| 5524 | continue; |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 5525 | } |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5526 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { |
| 5527 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { |
| 5528 | continue; |
| 5529 | } |
| 5530 | sp<AudioPolicyMix> primaryMix; |
| 5531 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->uid(), |
| 5532 | client->flags(), primaryMix, nullptr); |
| 5533 | if (status != OK) { |
| 5534 | continue; |
| 5535 | } |
yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 5536 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5537 | invalidate = true; |
| 5538 | if (desc->isStrategyActive(psId)) { |
| 5539 | maxLatency = desc->latency(); |
| 5540 | } |
| 5541 | break; |
| 5542 | } |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 5543 | } |
| 5544 | } |
| 5545 | |
Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5546 | if (srcOutputs != dstOutputs || invalidate) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5547 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 5548 | // audio from invalidated tracks will be rendered when unmuting |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5549 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 5550 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5551 | if (desc == nullptr) continue; |
| 5552 | |
| 5553 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5554 | maxLatency = desc->latency(); |
| 5555 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5556 | |
| 5557 | if (invalidate) continue; |
| 5558 | |
| 5559 | for (auto client : desc->clientsList(false /*activeOnly*/)) { |
Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 5560 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5561 | // a client on a non direct outputs has necessarily a linear PCM format |
| 5562 | // so we can call selectOutput() safely |
| 5563 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, |
| 5564 | client->flags(), |
| 5565 | client->config().format, |
| 5566 | client->config().channel_mask, |
jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 5567 | client->config().sample_rate, |
| 5568 | client->session()); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5569 | if (newOutput != srcOut) { |
| 5570 | invalidate = true; |
| 5571 | break; |
| 5572 | } |
| 5573 | } else { |
| 5574 | sp<IOProfile> profile = getProfileForOutput(newDevices, |
| 5575 | client->config().sample_rate, |
| 5576 | client->config().format, |
| 5577 | client->config().channel_mask, |
| 5578 | client->flags(), |
| 5579 | true /* directOnly */); |
| 5580 | if (profile != desc->mProfile) { |
| 5581 | invalidate = true; |
| 5582 | break; |
| 5583 | } |
| 5584 | } |
| 5585 | } |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5586 | } |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5587 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5588 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5589 | "%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] | 5590 | std::to_string(srcOutputs[0]).c_str(), |
| 5591 | std::to_string(dstOutputs[0]).c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5592 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5593 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5594 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5595 | if (desc == nullptr) continue; |
| 5596 | |
| 5597 | if (desc->isStrategyActive(psId)) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5598 | setStrategyMute(psId, true, desc); |
| 5599 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5600 | newDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5601 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5602 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5603 | if (source != nullptr && !isCallRxAudioSource(source)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5604 | connectAudioSource(source); |
| 5605 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5606 | } |
| 5607 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5608 | // Move effects associated to this stream from previous output to new output |
| 5609 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5610 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5611 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5612 | // 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] | 5613 | if (invalidate) { |
| 5614 | for (auto stream : mEngine->getStreamTypesForProductStrategy(psId)) { |
| 5615 | mpClientInterface->invalidateStream(stream); |
| 5616 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5617 | } |
| 5618 | } |
| 5619 | } |
| 5620 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5621 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5622 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5623 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { |
| 5624 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); |
| 5625 | checkOutputForAttributes(attributes); |
Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5626 | checkAudioSourceForAttributes(attributes); |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5627 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5628 | } |
| 5629 | |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5630 | void AudioPolicyManager::checkSecondaryOutputs() { |
| 5631 | std::set<audio_stream_type_t> streamsToInvalidate; |
| 5632 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5633 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; |
| 5634 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5635 | sp<AudioPolicyMix> primaryMix; |
| 5636 | std::vector<sp<AudioPolicyMix>> secondaryMixes; |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 5637 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->uid(), |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5638 | client->flags(), primaryMix, &secondaryMixes); |
| 5639 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; |
| 5640 | for (auto &secondaryMix : secondaryMixes) { |
| 5641 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); |
| 5642 | if (outputDesc != nullptr && |
| 5643 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { |
| 5644 | secondaryDescs.push_back(outputDesc); |
| 5645 | } |
| 5646 | } |
| 5647 | |
Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 5648 | if (status != OK || |
| 5649 | !std::equal(client->getSecondaryOutputs().begin(), |
Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5650 | client->getSecondaryOutputs().end(), |
| 5651 | secondaryDescs.begin(), secondaryDescs.end())) { |
| 5652 | streamsToInvalidate.insert(client->stream()); |
| 5653 | } |
| 5654 | } |
| 5655 | } |
| 5656 | for (audio_stream_type_t stream : streamsToInvalidate) { |
| 5657 | ALOGD("%s Invalidate stream %d due to secondary output change", __func__, stream); |
| 5658 | mpClientInterface->invalidateStream(stream); |
| 5659 | } |
| 5660 | } |
| 5661 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5662 | bool AudioPolicyManager::isScoRequestedForComm() const { |
| 5663 | AudioDeviceTypeAddrVector devices; |
| 5664 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); |
| 5665 | for (const auto &device : devices) { |
| 5666 | if (audio_is_bluetooth_out_sco_device(device.mType)) { |
| 5667 | return true; |
| 5668 | } |
| 5669 | } |
| 5670 | return false; |
| 5671 | } |
| 5672 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5673 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5674 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5675 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 5676 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5677 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5678 | return; |
| 5679 | } |
| 5680 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5681 | bool isScoConnected = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5682 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || |
| 5683 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5684 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5685 | |
| 5686 | // if suspended, restore A2DP output if: |
| 5687 | // ((SCO device is NOT connected) || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5688 | // ((SCO is not requested) && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5689 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5690 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5691 | // if not suspended, suspend A2DP output if: |
| 5692 | // (SCO device is connected) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5693 | // ((SCO is requested) || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5694 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5695 | // |
| 5696 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5697 | if (!isScoConnected || |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5698 | (!isScoRequested && |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5699 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5700 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5701 | |
| 5702 | mpClientInterface->restoreOutput(a2dpOutput); |
| 5703 | mA2dpSuspended = false; |
| 5704 | } |
| 5705 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5706 | if (isScoConnected && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5707 | (isScoRequested || |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5708 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5709 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5710 | |
| 5711 | mpClientInterface->suspendOutput(a2dpOutput); |
| 5712 | mA2dpSuspended = true; |
| 5713 | } |
| 5714 | } |
| 5715 | } |
| 5716 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5717 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 5718 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5719 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5720 | DeviceVector devices; |
| 5721 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5722 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5723 | if (index >= 0) { |
| 5724 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5725 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5726 | ALOGV("%s device %s forced by patch %d", __func__, |
| 5727 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 5728 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5729 | } |
| 5730 | } |
| 5731 | |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 5732 | // Do not retrieve engine device for outputs through MSD |
| 5733 | // TODO: support explicit routing requests by resetting MSD patch to engine device. |
| 5734 | if (outputDesc->devices() == getMsdAudioOutDevices()) { |
| 5735 | return outputDesc->devices(); |
| 5736 | } |
| 5737 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5738 | // Honor explicit routing requests only if no client using default routing is active on this |
| 5739 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 5740 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5741 | sp<DeviceDescriptor> device = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5742 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5743 | if (device != nullptr) { |
| 5744 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 5745 | } |
| 5746 | |
François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 5747 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict |
| 5748 | // of setForceUse / Default Bus device here |
| 5749 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); |
| 5750 | if (device != nullptr) { |
| 5751 | return DeviceVector(device); |
| 5752 | } |
| 5753 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5754 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { |
| 5755 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); |
| 5756 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 5757 | auto hasStreamActive = [&](auto stream) { |
| 5758 | return hasStream(streams, stream) && isStreamActive(stream, 0); |
| 5759 | }; |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5760 | |
Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 5761 | auto doGetOutputDevicesForVoice = [&]() { |
| 5762 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || |
| 5763 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) && |
| 5764 | (isInCall() || |
| 5765 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)); |
| 5766 | }; |
| 5767 | |
| 5768 | // With low-latency playing on speaker, music on WFD, when the first low-latency |
| 5769 | // output is stopped, getNewOutputDevices checks for a product strategy |
| 5770 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. |
| 5771 | // If an ALARM or ENFORCED_AUDIBLE stream is supported by the product strategy, |
| 5772 | // devices are returned for STRATEGY_SONIFICATION without checking whether the |
| 5773 | // stream is associated to the output descriptor. |
| 5774 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || |
| 5775 | ((hasStreamActive(AUDIO_STREAM_ALARM) || |
| 5776 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 5777 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5778 | // Retrieval of devices for voice DL is done on primary output profile, cannot |
| 5779 | // check the route (would force modifying configuration file for this profile) |
| 5780 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); |
| 5781 | break; |
| 5782 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5783 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5784 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5785 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5786 | } |
| 5787 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5788 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 5789 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5790 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5791 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5792 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5793 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5794 | if (index >= 0) { |
| 5795 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5796 | if (patchDesc->getUid() != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5797 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 5798 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 5799 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5800 | } |
| 5801 | } |
| 5802 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5803 | // Honor explicit routing requests only if no client using default routing is active on this |
| 5804 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 5805 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5806 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 5807 | if (device != nullptr) { |
| 5808 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5809 | } |
| 5810 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5811 | // 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] | 5812 | // a null sp<>, causing the patch on the input stream to be released. |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5813 | audio_attributes_t attributes = inputDesc->getHighestPriorityAttributes(); |
| 5814 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 5815 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5816 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5817 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5818 | device = mEngine->getInputDeviceForAttributes(attributes); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5819 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5820 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5821 | return device; |
| 5822 | } |
| 5823 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5824 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 5825 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 5826 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5827 | } |
| 5828 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5829 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5830 | // By checking the range of stream before calling getStrategy, we avoid |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5831 | // getOutputDevicesForStream's behavior for invalid streams. |
| 5832 | // engine's getOutputDevicesForStream would fallback on its default behavior (most probably |
| 5833 | // device for music stream), but we want to return the empty set. |
| 5834 | if (stream < AUDIO_STREAM_MIN || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5835 | return AUDIO_DEVICE_NONE; |
| 5836 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5837 | DeviceVector activeDevices; |
| 5838 | DeviceVector devices; |
Mikhail Naganov | f33115d | 2020-09-25 23:03:05 +0000 | [diff] [blame] | 5839 | for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_PUBLIC_CNT; ++i) { |
| 5840 | const audio_stream_type_t curStream{static_cast<audio_stream_type_t>(i)}; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5841 | if (!streamsMatchForvolume(stream, curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5842 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5843 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5844 | DeviceVector curDevices = mEngine->getOutputDevicesForStream(curStream, false/*fromCache*/); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5845 | devices.merge(curDevices); |
| 5846 | for (audio_io_handle_t output : getOutputsForDevices(curDevices, mOutputs)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5847 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 5848 | if (outputDesc->isActive(toVolumeSource(curStream))) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5849 | activeDevices.merge(outputDesc->devices()); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5850 | } |
| 5851 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5852 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5853 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 5854 | // Favor devices selected on active streams if any to report correct device in case of |
| 5855 | // explicit device selection |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5856 | if (!activeDevices.isEmpty()) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 5857 | devices = activeDevices; |
| 5858 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5859 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 5860 | and doesn't really need to.*/ |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5861 | DeviceVector speakerSafeDevices = devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5862 | if (!speakerSafeDevices.isEmpty()) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5863 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5864 | devices.remove(speakerSafeDevices); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5865 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5866 | // FIXME: use DeviceTypeSet when Java layer is ready for it. |
| 5867 | return deviceTypesToBitMask(devices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5868 | } |
| 5869 | |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5870 | status_t AudioPolicyManager::getDevicesForAttributes( |
| 5871 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices) { |
| 5872 | if (devices == nullptr) { |
| 5873 | return BAD_VALUE; |
| 5874 | } |
| 5875 | // check dynamic policies but only for primary descriptors (secondary not used for audible |
| 5876 | // audio routing, only used for duplication for playback capture) |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5877 | sp<AudioPolicyMix> policyMix; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5878 | status_t status = mPolicyMixes.getOutputForAttr(attr, 0 /*uid unknown here*/, |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5879 | AUDIO_OUTPUT_FLAG_NONE, policyMix, nullptr); |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5880 | if (status != OK) { |
| 5881 | return status; |
| 5882 | } |
Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5883 | if (policyMix != nullptr && policyMix->getOutput() != nullptr) { |
| 5884 | AudioDeviceTypeAddr device(policyMix->mDeviceType, policyMix->mDeviceAddress.c_str()); |
| 5885 | devices->push_back(device); |
| 5886 | return NO_ERROR; |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5887 | } |
Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5888 | DeviceVector curDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); |
| 5889 | for (const auto& device : curDevices) { |
| 5890 | devices->push_back(device->getDeviceTypeAddr()); |
| 5891 | } |
| 5892 | return NO_ERROR; |
| 5893 | } |
| 5894 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5895 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5896 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5897 | case AUDIO_STREAM_MUSIC: |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5898 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5899 | updateDevicesAndOutputs(); |
| 5900 | break; |
| 5901 | default: |
| 5902 | break; |
| 5903 | } |
| 5904 | } |
| 5905 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5906 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5907 | |
| 5908 | // skip beacon mute management if a dedicated TTS output is available |
| 5909 | if (mTtsOutputAvailable) { |
| 5910 | return 0; |
| 5911 | } |
| 5912 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5913 | switch(event) { |
| 5914 | case STARTING_OUTPUT: |
| 5915 | mBeaconMuteRefCount++; |
| 5916 | break; |
| 5917 | case STOPPING_OUTPUT: |
| 5918 | if (mBeaconMuteRefCount > 0) { |
| 5919 | mBeaconMuteRefCount--; |
| 5920 | } |
| 5921 | break; |
| 5922 | case STARTING_BEACON: |
| 5923 | mBeaconPlayingRefCount++; |
| 5924 | break; |
| 5925 | case STOPPING_BEACON: |
| 5926 | if (mBeaconPlayingRefCount > 0) { |
| 5927 | mBeaconPlayingRefCount--; |
| 5928 | } |
| 5929 | break; |
| 5930 | } |
| 5931 | |
| 5932 | if (mBeaconMuteRefCount > 0) { |
| 5933 | // any playback causes beacon to be muted |
| 5934 | return setBeaconMute(true); |
| 5935 | } else { |
| 5936 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5937 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5938 | } |
| 5939 | } |
| 5940 | |
| 5941 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5942 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5943 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5944 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5945 | if (mBeaconMuted != mute) { |
| 5946 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5947 | ALOGV("\t muting %d", mute); |
| 5948 | uint32_t maxLatency = 0; |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 5949 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5950 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5951 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5952 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5953 | const uint32_t latency = desc->latency() * 2; |
Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 5954 | if (desc->isActive(latency * 2) && latency > maxLatency) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5955 | maxLatency = latency; |
| 5956 | } |
| 5957 | } |
| 5958 | mBeaconMuted = mute; |
| 5959 | return maxLatency; |
| 5960 | } |
| 5961 | return 0; |
| 5962 | } |
| 5963 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5964 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5965 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5966 | mEngine->updateDeviceSelectionCache(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5967 | mPreviousOutputs = mOutputs; |
| 5968 | } |
| 5969 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5970 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5971 | const DeviceVector &prevDevices, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5972 | uint32_t delayMs) |
| 5973 | { |
| 5974 | // mute/unmute strategies using an incompatible device combination |
| 5975 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5976 | // if unmuting, unmute only after the specified delay |
| 5977 | if (outputDesc->isDuplicated()) { |
| 5978 | return 0; |
| 5979 | } |
| 5980 | |
| 5981 | uint32_t muteWaitMs = 0; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5982 | DeviceVector devices = outputDesc->devices(); |
| 5983 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5984 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5985 | auto productStrategies = mEngine->getOrderedProductStrategies(); |
| 5986 | for (const auto &productStrategy : productStrategies) { |
| 5987 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); |
| 5988 | DeviceVector curDevices = |
| 5989 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); |
| 5990 | curDevices = curDevices.filter(outputDesc->supportedDevices()); |
| 5991 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5992 | bool doMute = false; |
| 5993 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5994 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5995 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5996 | outputDesc->setStrategyMutedByDevice(productStrategy, true); |
| 5997 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5998 | doMute = true; |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5999 | outputDesc->setStrategyMutedByDevice(productStrategy, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6000 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6001 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6002 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6003 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6004 | // 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] | 6005 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6006 | continue; |
| 6007 | } |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6008 | ALOGVV("%s() %s (curDevice %s)", __func__, |
| 6009 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); |
| 6010 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); |
| 6011 | if (desc->isStrategyActive(productStrategy)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6012 | if (mute) { |
| 6013 | // FIXME: should not need to double latency if volume could be applied |
| 6014 | // immediately by the audioflinger mixer. We must account for the delay |
| 6015 | // between now and the next time the audioflinger thread for this output |
| 6016 | // will process a buffer (which corresponds to one buffer size, |
| 6017 | // usually 1/2 or 1/4 of the latency). |
| 6018 | if (muteWaitMs < desc->latency() * 2) { |
| 6019 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6020 | } |
| 6021 | } |
| 6022 | } |
| 6023 | } |
| 6024 | } |
| 6025 | } |
| 6026 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6027 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 6028 | // different per device volumes |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6029 | if (outputDesc->isActive() && (devices != prevDevices)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 6030 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 6031 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 6032 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 6033 | if (muteWaitMs < tempMuteWaitMs) { |
| 6034 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6035 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6036 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { |
| 6037 | // make sure that we do not start the temporary mute period too early in case of |
| 6038 | // delayed device change |
| 6039 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); |
| 6040 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6041 | devices.types()); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6042 | } |
| 6043 | } |
| 6044 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6045 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 6046 | if (muteWaitMs > delayMs) { |
| 6047 | muteWaitMs -= delayMs; |
| 6048 | usleep(muteWaitMs * 1000); |
| 6049 | return muteWaitMs; |
| 6050 | } |
| 6051 | return 0; |
| 6052 | } |
| 6053 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6054 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 6055 | const DeviceVector &devices, |
| 6056 | bool force, |
| 6057 | int delayMs, |
| 6058 | audio_patch_handle_t *patchHandle, |
| 6059 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6060 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6061 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6062 | uint32_t muteWaitMs; |
| 6063 | |
| 6064 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6065 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 6066 | nullptr /* patchHandle */, requiresMuteCheck); |
| 6067 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 6068 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6069 | return muteWaitMs; |
| 6070 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6071 | |
| 6072 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6073 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 6074 | DeviceVector prevDevices = outputDesc->devices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6075 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6076 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 6077 | |
| 6078 | if (!filteredDevices.isEmpty()) { |
| 6079 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6080 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6081 | |
| 6082 | // if the outputs are not materially active, there is no need to mute. |
| 6083 | if (requiresMuteCheck) { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6084 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6085 | } else { |
| 6086 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 6087 | muteWaitMs = 0; |
| 6088 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6089 | |
Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 6090 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 6091 | // output profile or if new device is not supported AND previous device(s) is(are) still |
| 6092 | // available (otherwise reset device must be done on the output) |
| 6093 | if (!devices.isEmpty() && filteredDevices.isEmpty() && |
| 6094 | !mAvailableOutputDevices.filter(prevDevices).empty()) { |
| 6095 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 6096 | // restore previous device after evaluating strategy mute state |
| 6097 | outputDesc->setDevices(prevDevices); |
| 6098 | return muteWaitMs; |
| 6099 | } |
| 6100 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6101 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 6102 | // the requested device is AUDIO_DEVICE_NONE |
| 6103 | // OR the requested device is the same as current device |
| 6104 | // AND force is not specified |
| 6105 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6106 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
Mikhail Naganov | 2d4e170 | 2019-01-24 12:59:44 -0800 | [diff] [blame] | 6107 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6108 | !force && outputDesc->getPatchHandle() != 0) { |
| 6109 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 6110 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6111 | return muteWaitMs; |
| 6112 | } |
| 6113 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6114 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6115 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6116 | // do the routing |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6117 | if (filteredDevices.isEmpty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6118 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6119 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6120 | PatchBuilder patchBuilder; |
| 6121 | patchBuilder.addSource(outputDesc); |
| 6122 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 6123 | for (const auto &filteredDevice : filteredDevices) { |
| 6124 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 6125 | } |
| 6126 | |
Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 6127 | // Add half reported latency to delayMs when muteWaitMs is null in order |
| 6128 | // to avoid disordered sequence of muting volume and changing devices. |
| 6129 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), |
| 6130 | muteWaitMs == 0 ? (delayMs + (outputDesc->latency() / 2)) : delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6131 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6132 | |
| 6133 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6134 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6135 | |
| 6136 | return muteWaitMs; |
| 6137 | } |
| 6138 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6139 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6140 | int delayMs, |
| 6141 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6142 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6143 | ssize_t index; |
| 6144 | if (patchHandle) { |
| 6145 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 6146 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 6147 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6148 | } |
| 6149 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6150 | return INVALID_OPERATION; |
| 6151 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6152 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6153 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6154 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 6155 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6156 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6157 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 6158 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6159 | return status; |
| 6160 | } |
| 6161 | |
| 6162 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6163 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6164 | bool force, |
| 6165 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6166 | { |
| 6167 | status_t status = NO_ERROR; |
| 6168 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6169 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6170 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 6171 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6172 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6173 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6174 | PatchBuilder patchBuilder; |
| 6175 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 6176 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 6177 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6178 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 6179 | auto result = usecase; |
| 6180 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 6181 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 6182 | } |
| 6183 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6184 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6185 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6186 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6187 | } |
| 6188 | } |
| 6189 | return status; |
| 6190 | } |
| 6191 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6192 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 6193 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6194 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6195 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6196 | ssize_t index; |
| 6197 | if (patchHandle) { |
| 6198 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 6199 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6200 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6201 | } |
| 6202 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6203 | return INVALID_OPERATION; |
| 6204 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6205 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6206 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6207 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 6208 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6209 | removeAudioPatch(patchDesc->getHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6210 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 6211 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6212 | return status; |
| 6213 | } |
| 6214 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6215 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6216 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6217 | audio_format_t& format, |
| 6218 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6219 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6220 | { |
| 6221 | // Choose an input profile based on the requested capture parameters: select the first available |
| 6222 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6223 | // |
| 6224 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 6225 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6226 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6227 | sp<IOProfile> firstInexact; |
| 6228 | uint32_t updatedSamplingRate = 0; |
| 6229 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 6230 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6231 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6232 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6233 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6234 | //updatedFormat = format; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6235 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6236 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6237 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6238 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6239 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6240 | &channelMask /*updatedChannelMask*/, |
| 6241 | // FIXME ugly cast |
| 6242 | (audio_output_flags_t) flags, |
| 6243 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6244 | return profile; |
| 6245 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6246 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6247 | samplingRate, |
| 6248 | &updatedSamplingRate, |
| 6249 | format, |
| 6250 | &updatedFormat, |
| 6251 | channelMask, |
| 6252 | &updatedChannelMask, |
| 6253 | // FIXME ugly cast |
| 6254 | (audio_output_flags_t) flags, |
| 6255 | false /*exactMatchRequiredForInputFlags*/)) { |
| 6256 | firstInexact = profile; |
| 6257 | } |
| 6258 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6259 | } |
| 6260 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6261 | if (firstInexact != nullptr) { |
| 6262 | samplingRate = updatedSamplingRate; |
| 6263 | format = updatedFormat; |
| 6264 | channelMask = updatedChannelMask; |
| 6265 | return firstInexact; |
| 6266 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6267 | return NULL; |
| 6268 | } |
| 6269 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6270 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, |
| 6271 | VolumeSource volumeSource, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6272 | int index, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6273 | const DeviceTypeSet& deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6274 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6275 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 6276 | |
| 6277 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 6278 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 6279 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 6280 | // the ringtone volume |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6281 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL); |
| 6282 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING); |
| 6283 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC); |
| 6284 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM); |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6285 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6286 | |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6287 | if (volumeSource == a11yVolumeSrc |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6288 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && |
| 6289 | mOutputs.isActive(ringVolumeSrc, 0)) { |
| 6290 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6291 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6292 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 6293 | } |
| 6294 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 6295 | // in-call: always cap volume by voice volume + some low headroom |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6296 | if ((volumeSource != callVolumeSrc && (isInCall() || |
| 6297 | mOutputs.isActiveLocally(callVolumeSrc))) && |
| 6298 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM) || |
| 6299 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || |
| 6300 | volumeSource == alarmVolumeSrc || |
| 6301 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION) || |
| 6302 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE) || |
| 6303 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF) || |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6304 | volumeSource == a11yVolumeSrc)) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6305 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6306 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6307 | const float maxVoiceVolDb = |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6308 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 6309 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 6310 | // FIXME: Workaround for call screening applications until a proper audio mode is defined |
| 6311 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was |
| 6312 | // programmatically muted. |
| 6313 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to |
| 6314 | // 0. We don't want to cap volume when the system has programmatically muted the voice call |
| 6315 | // stream. See setVolumeCurveIndex() for more information. |
Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6316 | bool exemptFromCapping = |
| 6317 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) |
| 6318 | && (voiceVolumeIndex == 0); |
Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 6319 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, |
| 6320 | volumeSource, volumeDb); |
| 6321 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6322 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, |
| 6323 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); |
| 6324 | volumeDb = maxVoiceVolDb; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 6325 | } |
| 6326 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6327 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 6328 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 6329 | // - always attenuate notifications volume by 6dB |
| 6330 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 6331 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6332 | // - if music is playing, always limit the volume to current music volume, |
| 6333 | // with a minimum threshold at -36dB so that notification is always perceived. |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6334 | if (!Intersection(deviceTypes, |
| 6335 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, |
| 6336 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, |
Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 6337 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, |
| 6338 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6339 | ((volumeSource == alarmVolumeSrc || |
| 6340 | volumeSource == ringVolumeSrc) || |
| 6341 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION)) || |
| 6342 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM)) || |
| 6343 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE)) && |
| 6344 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 6345 | curves.canBeMuted()) { |
| 6346 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6347 | // when the phone is ringing we must consider that music could have been paused just before |
| 6348 | // by the music application and behave as if music was active if the last music track was |
| 6349 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6350 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6351 | mLimitRingtoneVolume) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6352 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6353 | DeviceTypeSet musicDevice = |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6354 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), |
| 6355 | nullptr, true /*fromCache*/).types(); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6356 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6357 | float musicVolDb = computeVolume(musicCurves, |
| 6358 | musicVolumeSrc, |
| 6359 | musicCurves.getVolumeIndex(musicDevice), |
| 6360 | musicDevice); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6361 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 6362 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 6363 | if (volumeDb > minVolDb) { |
| 6364 | volumeDb = minVolDb; |
| 6365 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6366 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6367 | if (!Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, |
| 6368 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 6369 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 6370 | // intended to be played |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6371 | if ((volumeDb > -96.0f) && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6372 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6373 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", |
| 6374 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6375 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 6376 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 6377 | } |
| 6378 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6379 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6380 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6381 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6382 | } |
| 6383 | } |
| 6384 | |
François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6385 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6386 | } |
| 6387 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6388 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 6389 | VolumeSource fromVolumeSource, |
| 6390 | VolumeSource toVolumeSource) |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6391 | { |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 6392 | if (fromVolumeSource == toVolumeSource) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6393 | return srcIndex; |
| 6394 | } |
Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 6395 | auto &srcCurves = getVolumeCurves(fromVolumeSource); |
| 6396 | auto &dstCurves = getVolumeCurves(toVolumeSource); |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6397 | float minSrc = (float)srcCurves.getVolumeIndexMin(); |
| 6398 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); |
| 6399 | float minDst = (float)dstCurves.getVolumeIndexMin(); |
| 6400 | float maxDst = (float)dstCurves.getVolumeIndexMax(); |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6401 | |
Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 6402 | // preserve mute request or correct range |
| 6403 | if (srcIndex < minSrc) { |
| 6404 | if (srcIndex == 0) { |
| 6405 | return 0; |
| 6406 | } |
| 6407 | srcIndex = minSrc; |
| 6408 | } else if (srcIndex > maxSrc) { |
| 6409 | srcIndex = maxSrc; |
| 6410 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6411 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 6412 | } |
| 6413 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6414 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, |
| 6415 | VolumeSource volumeSource, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6416 | int index, |
| 6417 | const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6418 | DeviceTypeSet deviceTypes, |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6419 | int delayMs, |
| 6420 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6421 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6422 | // do not change actual attributes volume if the attributes is muted |
| 6423 | if (outputDesc->isMuted(volumeSource)) { |
| 6424 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, |
| 6425 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6426 | return NO_ERROR; |
| 6427 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6428 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL); |
| 6429 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO); |
| 6430 | bool isVoiceVolSrc = callVolSrc == volumeSource; |
| 6431 | bool isBtScoVolSrc = btScoVolSrc == volumeSource; |
| 6432 | |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6433 | bool isScoRequested = isScoRequestedForComm(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6434 | // 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] | 6435 | // if sco and call follow same curves, bypass forceUseForComm |
| 6436 | if ((callVolSrc != btScoVolSrc) && |
Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6437 | ((isVoiceVolSrc && isScoRequested) || |
| 6438 | (isBtScoVolSrc && !isScoRequested))) { |
| 6439 | ALOGV("%s cannot set volume group %d volume when is%srequested for comm", __func__, |
| 6440 | volumeSource, isScoRequested ? " " : "n ot "); |
Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 6441 | // Do not return an error here as AudioService will always set both voice call |
| 6442 | // and bluetooth SCO volumes due to stream aliasing. |
| 6443 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6444 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6445 | if (deviceTypes.empty()) { |
| 6446 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6447 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6448 | |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6449 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); |
| 6450 | if (outputDesc->isFixedVolume(deviceTypes) || |
Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 6451 | // Force VoIP volume to max for bluetooth SCO device except if muted |
| 6452 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6453 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 6454 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6455 | } |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6456 | outputDesc->setVolume( |
| 6457 | volumeDb, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6458 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6459 | if (isVoiceVolSrc || isBtScoVolSrc) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6460 | float voiceVolume; |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 6461 | // 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] | 6462 | if (isVoiceVolSrc) { |
| 6463 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6464 | } else { |
Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 6465 | voiceVolume = index == 0 ? 0.0 : 1.0; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6466 | } |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 6467 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6468 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 6469 | mLastVoiceVolume = voiceVolume; |
| 6470 | } |
| 6471 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6472 | return NO_ERROR; |
| 6473 | } |
| 6474 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6475 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6476 | const DeviceTypeSet& deviceTypes, |
| 6477 | int delayMs, |
| 6478 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6479 | { |
jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 6480 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6481 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { |
| 6482 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); |
| 6483 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6484 | curves.getVolumeIndex(deviceTypes), |
| 6485 | outputDesc, deviceTypes, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6486 | } |
| 6487 | } |
| 6488 | |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6489 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, |
| 6490 | bool on, |
| 6491 | const sp<AudioOutputDescriptor>& outputDesc, |
| 6492 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6493 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6494 | { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6495 | std::vector<VolumeSource> sourcesToMute; |
| 6496 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { |
| 6497 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, |
| 6498 | toString(attributes).c_str(), on, outputDesc->getId()); |
| 6499 | VolumeSource source = toVolumeSource(attributes); |
| 6500 | if (std::find(begin(sourcesToMute), end(sourcesToMute), source) == end(sourcesToMute)) { |
| 6501 | sourcesToMute.push_back(source); |
| 6502 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6503 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6504 | for (auto source : sourcesToMute) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6505 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6506 | } |
| 6507 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6508 | } |
| 6509 | |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6510 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, |
| 6511 | bool on, |
| 6512 | const sp<AudioOutputDescriptor>& outputDesc, |
| 6513 | int delayMs, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6514 | DeviceTypeSet deviceTypes) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6515 | { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6516 | if (deviceTypes.empty()) { |
| 6517 | deviceTypes = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6518 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6519 | auto &curves = getVolumeCurves(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6520 | if (on) { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6521 | if (!outputDesc->isMuted(volumeSource)) { |
Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6522 | if (curves.canBeMuted() && |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6523 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE) || |
| 6524 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == |
| 6525 | AUDIO_POLICY_FORCE_NONE))) { |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6526 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6527 | } |
| 6528 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6529 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not |
| 6530 | // ignored |
| 6531 | outputDesc->incMuteCount(volumeSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6532 | } else { |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6533 | if (!outputDesc->isMuted(volumeSource)) { |
| 6534 | ALOGV("%s unmuting non muted attributes!", __func__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6535 | return; |
| 6536 | } |
François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6537 | if (outputDesc->decMuteCount(volumeSource) == 0) { |
| 6538 | checkAndSetVolume(curves, volumeSource, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6539 | curves.getVolumeIndex(deviceTypes), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6540 | outputDesc, |
jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6541 | deviceTypes, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6542 | delayMs); |
| 6543 | } |
| 6544 | } |
| 6545 | } |
| 6546 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6547 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 6548 | { |
François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6549 | // has flags that map to a stream type? |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 6550 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 6551 | return true; |
| 6552 | } |
| 6553 | |
| 6554 | // has known usage? |
| 6555 | switch (paa->usage) { |
| 6556 | case AUDIO_USAGE_UNKNOWN: |
| 6557 | case AUDIO_USAGE_MEDIA: |
| 6558 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 6559 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 6560 | case AUDIO_USAGE_ALARM: |
| 6561 | case AUDIO_USAGE_NOTIFICATION: |
| 6562 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 6563 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 6564 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 6565 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 6566 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 6567 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 6568 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 6569 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 6570 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6571 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 6572 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 6573 | case AUDIO_USAGE_CALL_ASSISTANT: |
Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 6574 | case AUDIO_USAGE_EMERGENCY: |
| 6575 | case AUDIO_USAGE_SAFETY: |
| 6576 | case AUDIO_USAGE_VEHICLE_STATUS: |
| 6577 | case AUDIO_USAGE_ANNOUNCEMENT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 6578 | break; |
| 6579 | default: |
| 6580 | return false; |
| 6581 | } |
| 6582 | return true; |
| 6583 | } |
| 6584 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6585 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 6586 | { |
| 6587 | return mEngine->getForceUse(usage); |
| 6588 | } |
| 6589 | |
| 6590 | bool AudioPolicyManager::isInCall() |
| 6591 | { |
| 6592 | return isStateInCall(mEngine->getPhoneState()); |
| 6593 | } |
| 6594 | |
| 6595 | bool AudioPolicyManager::isStateInCall(int state) |
| 6596 | { |
| 6597 | return is_state_in_call(state); |
| 6598 | } |
| 6599 | |
Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 6600 | bool AudioPolicyManager::isCallAudioAccessible() |
| 6601 | { |
| 6602 | audio_mode_t mode = mEngine->getPhoneState(); |
| 6603 | return (mode == AUDIO_MODE_IN_CALL) |
| 6604 | || (mode == AUDIO_MODE_IN_COMMUNICATION) |
| 6605 | || (mode == AUDIO_MODE_CALL_SCREEN); |
| 6606 | } |
| 6607 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6608 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 6609 | { |
| 6610 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6611 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6612 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || |
Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6613 | sourceDesc->sinkDevice()->equals(deviceDesc)) |
| 6614 | && !isCallRxAudioSource(sourceDesc)) { |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6615 | disconnectAudioSource(sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6616 | } |
| 6617 | } |
| 6618 | |
| 6619 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 6620 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 6621 | bool release = false; |
| 6622 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 6623 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 6624 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 6625 | source->ext.device.type == deviceDesc->type()) { |
| 6626 | release = true; |
| 6627 | } |
| 6628 | } |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6629 | const char *address = deviceDesc->address().c_str(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6630 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 6631 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 6632 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6633 | sink->ext.device.type == deviceDesc->type() && |
| 6634 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 |
| 6635 | || strncmp(sink->ext.device.address, address, |
| 6636 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6637 | release = true; |
| 6638 | } |
| 6639 | } |
| 6640 | if (release) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6641 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); |
| 6642 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6643 | } |
| 6644 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6645 | |
Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 6646 | mInputs.clearSessionRoutesForDevice(deviceDesc); |
| 6647 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6648 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6649 | } |
| 6650 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6651 | void AudioPolicyManager::modifySurroundFormats( |
| 6652 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6653 | std::unordered_set<audio_format_t> enforcedSurround( |
| 6654 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6655 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 6656 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 6657 | allSurround.insert(pair.first); |
| 6658 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 6659 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6660 | |
| 6661 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 6662 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6663 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6664 | // This is the resulting set of formats depending on the surround mode: |
| 6665 | // 'all surround' = allSurround |
| 6666 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 6667 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 6668 | // 'manual surround' = mManualSurroundFormats |
| 6669 | // AUTO: formats v 'enforced surround' |
| 6670 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 6671 | // NEVER: formats ^ 'non-surround' |
| 6672 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6673 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6674 | std::unordered_set<audio_format_t> formatSet; |
| 6675 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 6676 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6677 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6678 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6679 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6680 | formatSet.insert(*formatIter); |
| 6681 | } |
| 6682 | } |
| 6683 | } else { |
| 6684 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 6685 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6686 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6687 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6688 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6689 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6690 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 6691 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 6692 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6693 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6694 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 6695 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 6696 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6697 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6698 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6699 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6700 | for (const auto& format : formatSet) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6701 | formatsPtr->push_back(format); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6702 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6703 | } |
| 6704 | |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6705 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { |
| 6706 | ChannelMaskSet &channelMasks = *channelMasksPtr; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6707 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 6708 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 6709 | |
| 6710 | // If NEVER, then remove support for channelMasks > stereo. |
| 6711 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6712 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { |
| 6713 | audio_channel_mask_t channelMask = *it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6714 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6715 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6716 | it = channelMasks.erase(it); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6717 | } else { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6718 | ++it; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6719 | } |
| 6720 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6721 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 6722 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 6723 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6724 | bool supports5dot1 = false; |
| 6725 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6726 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6727 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 6728 | supports5dot1 = true; |
| 6729 | break; |
| 6730 | } |
| 6731 | } |
| 6732 | // If not then add 5.1 support. |
| 6733 | if (!supports5dot1) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6734 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); |
Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6735 | 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] | 6736 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6737 | } |
| 6738 | } |
| 6739 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6740 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6741 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6742 | AudioProfileVector &profiles) |
| 6743 | { |
| 6744 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6745 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6746 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6747 | // Format MUST be checked first to update the list of AudioProfile |
| 6748 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6749 | reply = mpClientInterface->getParameters( |
| 6750 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6751 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6752 | AudioParameter repliedParameters(reply); |
| 6753 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6754 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6755 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 6756 | return; |
| 6757 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6758 | FormatVector formats = formatsFromString(reply.string()); |
Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 6759 | mReportedFormatsMap[devDesc] = formats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6760 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 6761 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6762 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6763 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 6764 | addProfilesForFormats(profiles, formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6765 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6766 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6767 | for (audio_format_t format : profiles.getSupportedFormats()) { |
jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6768 | ChannelMaskSet channelMasks; |
| 6769 | SampleRateSet samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6770 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6771 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6772 | |
| 6773 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6774 | reply = mpClientInterface->getParameters( |
| 6775 | ioHandle, |
| 6776 | requestedParameters.toString() + ";" + |
| 6777 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6778 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6779 | AudioParameter repliedParameters(reply); |
| 6780 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6781 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6782 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6783 | } |
| 6784 | } |
| 6785 | if (profiles.hasDynamicChannelsFor(format)) { |
| 6786 | reply = mpClientInterface->getParameters(ioHandle, |
| 6787 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6788 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6789 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6790 | AudioParameter repliedParameters(reply); |
| 6791 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6792 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6793 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6794 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 6795 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6796 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6797 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6798 | } |
| 6799 | } |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 6800 | addDynamicAudioProfileAndSort( |
| 6801 | profiles, new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6802 | } |
| 6803 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6804 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6805 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6806 | audio_patch_handle_t *patchHandle, |
| 6807 | AudioIODescriptorInterface *ioDescriptor, |
| 6808 | const struct audio_patch *patch, |
| 6809 | int delayMs) |
| 6810 | { |
| 6811 | ssize_t index = mAudioPatches.indexOfKey( |
| 6812 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6813 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6814 | sp<AudioPatch> patchDesc; |
| 6815 | status_t status = installPatch( |
| 6816 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6817 | if (status == NO_ERROR) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6818 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6819 | } |
| 6820 | return status; |
| 6821 | } |
| 6822 | |
| 6823 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6824 | ssize_t index, |
| 6825 | audio_patch_handle_t *patchHandle, |
| 6826 | const struct audio_patch *patch, |
| 6827 | int delayMs, |
| 6828 | uid_t uid, |
| 6829 | sp<AudioPatch> *patchDescPtr) |
| 6830 | { |
| 6831 | sp<AudioPatch> patchDesc; |
| 6832 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6833 | if (index >= 0) { |
| 6834 | patchDesc = mAudioPatches.valueAt(index); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6835 | afPatchHandle = patchDesc->getAfHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6839 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6840 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6841 | if (status == NO_ERROR) { |
| 6842 | if (index < 0) { |
| 6843 | patchDesc = new AudioPatch(patch, uid); |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6844 | addAudioPatch(patchDesc->getHandle(), patchDesc); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6845 | } else { |
| 6846 | patchDesc->mPatch = *patch; |
| 6847 | } |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6848 | patchDesc->setAfHandle(afPatchHandle); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6849 | if (patchHandle) { |
François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6850 | *patchHandle = patchDesc->getHandle(); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6851 | } |
| 6852 | nextAudioPortGeneration(); |
| 6853 | mpClientInterface->onAudioPatchListUpdate(); |
| 6854 | } |
| 6855 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6856 | return status; |
| 6857 | } |
| 6858 | |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6859 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) |
| 6860 | { |
| 6861 | const TrackClientVector activeClients = output->getActiveClients(); |
| 6862 | if (activeClients.empty()) { |
| 6863 | return true; |
| 6864 | } |
| 6865 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); |
| 6866 | if (index < 0) { |
| 6867 | ALOGE("%s, no audio patch found while there are active clients on output %d", |
| 6868 | __func__, output->getId()); |
| 6869 | return false; |
| 6870 | } |
| 6871 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 6872 | DeviceVector routedDevices; |
| 6873 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { |
| 6874 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( |
| 6875 | patchDesc->mPatch.sinks[i].id); |
| 6876 | if (device == nullptr) { |
| 6877 | ALOGE("%s, no audio device found with id(%d)", |
| 6878 | __func__, patchDesc->mPatch.sinks[i].id); |
| 6879 | return false; |
| 6880 | } |
| 6881 | routedDevices.add(device); |
| 6882 | } |
| 6883 | for (const auto& client : activeClients) { |
| 6884 | // TODO: b/175343099 only travel the valid client |
| 6885 | sp<DeviceDescriptor> preferredDevice = |
| 6886 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); |
| 6887 | if (mEngine->getOutputDevicesForAttributes( |
| 6888 | client->attributes(), preferredDevice, false) == routedDevices) { |
| 6889 | return false; |
| 6890 | } |
| 6891 | } |
| 6892 | return true; |
| 6893 | } |
| 6894 | |
| 6895 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( |
| 6896 | const sp<IOProfile>& profile, const DeviceVector& devices) |
| 6897 | { |
| 6898 | for (const auto& device : devices) { |
| 6899 | // TODO: This should be checking if the profile supports the device combo. |
| 6900 | if (!profile->supportsDevice(device)) { |
| 6901 | return nullptr; |
| 6902 | } |
| 6903 | } |
| 6904 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 6905 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 6906 | status_t status = desc->open(nullptr, devices, |
| 6907 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
| 6908 | if (status != NO_ERROR) { |
| 6909 | return nullptr; |
| 6910 | } |
| 6911 | |
| 6912 | // Here is where the out_set_parameters() for card & device gets called |
| 6913 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); |
| 6914 | const audio_devices_t deviceType = device->type(); |
| 6915 | const String8 &address = String8(device->address().c_str()); |
| 6916 | if (!address.isEmpty()) { |
| 6917 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); |
| 6918 | mpClientInterface->setParameters(output, String8(param)); |
| 6919 | free(param); |
| 6920 | } |
| 6921 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
| 6922 | if (!profile->hasValidAudioProfile()) { |
| 6923 | ALOGW("%s() missing param", __func__); |
| 6924 | desc->close(); |
| 6925 | return nullptr; |
| 6926 | } else if (profile->hasDynamicAudioProfile()) { |
| 6927 | desc->close(); |
| 6928 | output = AUDIO_IO_HANDLE_NONE; |
| 6929 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 6930 | profile->pickAudioProfile( |
| 6931 | config.sample_rate, config.channel_mask, config.format); |
| 6932 | config.offload_info.sample_rate = config.sample_rate; |
| 6933 | config.offload_info.channel_mask = config.channel_mask; |
| 6934 | config.offload_info.format = config.format; |
| 6935 | |
| 6936 | status = desc->open(&config, devices, |
| 6937 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
| 6938 | if (status != NO_ERROR) { |
| 6939 | return nullptr; |
| 6940 | } |
| 6941 | } |
| 6942 | |
| 6943 | addOutput(output, desc); |
| 6944 | if (audio_is_remote_submix_device(deviceType) && address != "0") { |
| 6945 | sp<AudioPolicyMix> policyMix; |
| 6946 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { |
| 6947 | policyMix->setOutput(desc); |
| 6948 | desc->mPolicyMix = policyMix; |
| 6949 | } else { |
| 6950 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
| 6951 | address.string()); |
| 6952 | } |
| 6953 | |
| 6954 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && hasPrimaryOutput()) { |
| 6955 | // no duplicated output for direct outputs and |
| 6956 | // outputs used by dynamic policy mixes |
| 6957 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
| 6958 | |
| 6959 | //TODO: configure audio effect output stage here |
| 6960 | |
| 6961 | // open a duplicating output thread for the new output and the primary output |
| 6962 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 6963 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); |
| 6964 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); |
| 6965 | if (status == NO_ERROR) { |
| 6966 | // add duplicated output descriptor |
| 6967 | addOutput(duplicatedOutput, dupOutputDesc); |
| 6968 | } else { |
| 6969 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 6970 | mPrimaryOutput->mIoHandle, output); |
| 6971 | desc->close(); |
| 6972 | removeOutput(output); |
| 6973 | nextAudioPortGeneration(); |
| 6974 | return nullptr; |
| 6975 | } |
| 6976 | } |
Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 6977 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 6978 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); |
| 6979 | mPrimaryOutput = desc; |
| 6980 | } |
jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6981 | return desc; |
| 6982 | } |
| 6983 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6984 | } // namespace android |