| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2009 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" | 
| Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 18 |  | 
|  | 19 | // Need to keep the log statements even in production builds | 
|  | 20 | // to enable VERBOSE logging dynamically. | 
|  | 21 | // You can enable VERBOSE logging as follows: | 
|  | 22 | // adb shell setprop log.tag.APM_AudioPolicyManager V | 
|  | 23 | #define LOG_NDEBUG 0 | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 24 |  | 
|  | 25 | //#define VERY_VERBOSE_LOGGING | 
|  | 26 | #ifdef VERY_VERBOSE_LOGGING | 
|  | 27 | #define ALOGVV ALOGV | 
|  | 28 | #else | 
|  | 29 | #define ALOGVV(a...) do { } while(0) | 
|  | 30 | #endif | 
|  | 31 |  | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 32 | #include <algorithm> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 33 | #include <inttypes.h> | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 34 | #include <map> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 35 | #include <math.h> | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 36 | #include <set> | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 37 | #include <unordered_set> | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 38 | #include <vector> | 
| Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 39 |  | 
|  | 40 | #include <Serializer.h> | 
| Glenn Kasten | 76a1344 | 2020-07-01 12:10:59 -0700 | [diff] [blame] | 41 | #include <cutils/bitops.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 42 | #include <cutils/properties.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 43 | #include <media/AudioParameter.h> | 
| Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 44 | #include <policy.h> | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 45 | #include <private/android_filesystem_config.h> | 
| Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 46 | #include <system/audio.h> | 
| Mikhail Naganov | 27cf37c | 2020-04-14 14:47:01 -0700 | [diff] [blame] | 47 | #include <system/audio_config.h> | 
| jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 48 | #include <system/audio_effects/effect_hapticgenerator.h> | 
| Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 49 | #include <utils/Log.h> | 
|  | 50 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 51 | #include "AudioPolicyManager.h" | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 52 | #include "TypeConverter.h" | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 53 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 54 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 55 |  | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 56 | using content::AttributionSourceState; | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 57 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 58 | //FIXME: workaround for truncated touch sounds | 
|  | 59 | // to be removed when the problem is handled by system UI | 
|  | 60 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 | 
| Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 61 |  | 
|  | 62 | // Largest difference in dB on earpiece in call between the voice volume and another | 
|  | 63 | // media / notification / system volume. | 
|  | 64 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; | 
|  | 65 |  | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 66 | // Compressed formats for MSD module, ordered from most preferred to least preferred. | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 67 | static const std::vector<audio_format_t> msdCompressedFormatsOrder = {{ | 
|  | 68 | AUDIO_FORMAT_IEC60958, AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 69 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; | 
|  | 70 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 71 | static const std::vector<audio_channel_mask_t> msdSurroundChannelMasksOrder = {{ | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 72 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, | 
|  | 73 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, | 
|  | 74 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; | 
|  | 75 |  | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 76 | template <typename T> | 
|  | 77 | bool operator== (const SortedVector<T> &left, const SortedVector<T> &right) | 
|  | 78 | { | 
|  | 79 | if (left.size() != right.size()) { | 
|  | 80 | return false; | 
|  | 81 | } | 
|  | 82 | for (size_t index = 0; index < right.size(); index++) { | 
|  | 83 | if (left[index] != right[index]) { | 
|  | 84 | return false; | 
|  | 85 | } | 
|  | 86 | } | 
|  | 87 | return true; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | template <typename T> | 
|  | 91 | bool operator!= (const SortedVector<T> &left, const SortedVector<T> &right) | 
|  | 92 | { | 
|  | 93 | return !(left == right); | 
|  | 94 | } | 
|  | 95 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 96 | // ---------------------------------------------------------------------------- | 
|  | 97 | // AudioPolicyInterface implementation | 
|  | 98 | // ---------------------------------------------------------------------------- | 
|  | 99 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 100 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 101 | audio_policy_dev_state_t state, | 
|  | 102 | const char *device_address, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 103 | const char *device_name, | 
|  | 104 | audio_format_t encodedFormat) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 105 | { | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 106 | status_t status = setDeviceConnectionStateInt(device, state, device_address, | 
|  | 107 | device_name, encodedFormat); | 
| jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 108 | nextAudioPortGeneration(); | 
|  | 109 | return status; | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 112 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, | 
|  | 113 | audio_policy_dev_state_t state) | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 114 | { | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 115 | AudioParameter param(String8(device->address().c_str())); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 116 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? | 
| Mikhail Naganov | f23fcf6 | 2019-07-08 15:28:43 -0700 | [diff] [blame] | 117 | AudioParameter::keyDeviceConnect : AudioParameter::keyDeviceDisconnect); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 118 | param.addInt(key, device->type()); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 119 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 120 | } | 
|  | 121 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 122 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 123 | audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 124 | const char *device_address, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 125 | const char *device_name, | 
|  | 126 | audio_format_t encodedFormat) | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 127 | { | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 128 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s format 0x%X", | 
|  | 129 | deviceType, state, device_address, device_name, encodedFormat); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 130 |  | 
|  | 131 | // connect/disconnect only 1 device at a time | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 132 | 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] | 133 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 134 | sp<DeviceDescriptor> device = | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 135 | mHwModules.getDeviceDescriptor(deviceType, device_address, device_name, encodedFormat, | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 136 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 137 | return device ? setDeviceConnectionStateInt(device, state) : INVALID_OPERATION; | 
|  | 138 | } | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 139 |  | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 140 | status_t AudioPolicyManager::setDeviceConnectionStateInt(const sp<DeviceDescriptor> &device, | 
|  | 141 | audio_policy_dev_state_t state) | 
|  | 142 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 143 | // handle output devices | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 144 | if (audio_is_output_device(device->type())) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 145 | SortedVector <audio_io_handle_t> outputs; | 
|  | 146 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 147 | ssize_t index = mAvailableOutputDevices.indexOf(device); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 148 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 149 | // save a copy of the opened output descriptors before any output is opened or closed | 
|  | 150 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
|  | 151 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 152 | switch (state) | 
|  | 153 | { | 
|  | 154 | // handle output device connection | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 155 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 156 | if (index >= 0) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 157 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 158 | return INVALID_OPERATION; | 
|  | 159 | } | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 160 | ALOGV("%s() connecting device %s format %x", | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 161 | __func__, device->toString().c_str(), device->getEncodedFormat()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 162 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 163 | // register new device as available | 
| Francois Gaffie | 993f390 | 2019-04-10 15:39:27 +0200 | [diff] [blame] | 164 | if (mAvailableOutputDevices.add(device) < 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 165 | return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 168 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic | 
|  | 169 | // parameters on newly connected devices (instead of opening the outputs...) | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 170 | broadcastDeviceConnectionState(device, state); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 171 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 172 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { | 
|  | 173 | mAvailableOutputDevices.remove(device); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 174 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 175 | mHwModules.cleanUpForDevice(device); | 
|  | 176 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 177 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 178 | return INVALID_OPERATION; | 
|  | 179 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 180 |  | 
| jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 181 | // Populate encapsulation information when a output device is connected. | 
|  | 182 | device->setEncapsulationInfoFromHal(mpClientInterface); | 
|  | 183 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 184 | // outputs should never be empty here | 
|  | 185 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
|  | 186 | "checkOutputsForDevice() returned no outputs but status OK"); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 187 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 188 |  | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 189 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 190 | // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 191 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 192 | if (index < 0) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 193 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 194 | return INVALID_OPERATION; | 
|  | 195 | } | 
|  | 196 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 197 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 198 |  | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 199 | // Send Disconnect to HALs | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 200 | broadcastDeviceConnectionState(device, state); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 201 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 202 | // remove device from available output devices | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 203 | mAvailableOutputDevices.remove(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 204 |  | 
| Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 205 | mOutputs.clearSessionRoutesForDevice(device); | 
|  | 206 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 207 | checkOutputsForDevice(device, state, outputs); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 208 |  | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 209 | // Reset active device codec | 
|  | 210 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); | 
|  | 211 |  | 
| Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 212 | // remove device from mReportedFormatsMap cache | 
|  | 213 | mReportedFormatsMap.erase(device); | 
|  | 214 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 215 | } break; | 
|  | 216 |  | 
|  | 217 | default: | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 218 | ALOGE("%s() invalid state: %x", __func__, state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 219 | return BAD_VALUE; | 
|  | 220 | } | 
|  | 221 |  | 
| Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 222 | // Propagate device availability to Engine | 
|  | 223 | setEngineDeviceConnectionState(device, state); | 
|  | 224 |  | 
| Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 225 | // No need to evaluate playback routing when connecting a remote submix | 
|  | 226 | // output device used by a dynamic policy of type recorder as no | 
|  | 227 | // playback use case is affected. | 
|  | 228 | bool doCheckForDeviceAndOutputChanges = true; | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 229 | if (device->type() == AUDIO_DEVICE_OUT_REMOTE_SUBMIX && device->address() != "0") { | 
| Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 230 | for (audio_io_handle_t output : outputs) { | 
|  | 231 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 232 | sp<AudioPolicyMix> policyMix = desc->mPolicyMix.promote(); | 
|  | 233 | if (policyMix != nullptr | 
|  | 234 | && policyMix->mMixType == MIX_TYPE_RECORDERS | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 235 | && device->address() == policyMix->mDeviceAddress.string()) { | 
| Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 236 | doCheckForDeviceAndOutputChanges = false; | 
|  | 237 | break; | 
|  | 238 | } | 
|  | 239 | } | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | auto checkCloseOutputs = [&]() { | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 243 | // outputs must be closed after checkOutputForAllStrategies() is executed | 
|  | 244 | if (!outputs.isEmpty()) { | 
|  | 245 | for (audio_io_handle_t output : outputs) { | 
|  | 246 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 247 | // close unused outputs after device disconnection or direct outputs that have | 
|  | 248 | // been opened by checkOutputsForDevice() to query dynamic parameters | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 249 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
|  | 250 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
| Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 251 | (desc->mDirectOpenCount == 0))) { | 
| Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 252 | clearAudioSourcesForOutput(output); | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 253 | closeOutput(output); | 
|  | 254 | } | 
| 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 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed | 
|  | 257 | return true; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 258 | } | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 259 | return false; | 
| Eric Laurent | ae97002 | 2019-01-29 14:25:04 -0800 | [diff] [blame] | 260 | }; | 
|  | 261 |  | 
|  | 262 | if (doCheckForDeviceAndOutputChanges) { | 
|  | 263 | checkForDeviceAndOutputChanges(checkCloseOutputs); | 
|  | 264 | } else { | 
|  | 265 | checkCloseOutputs(); | 
|  | 266 | } | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 267 | (void)updateCallRouting(false /*fromCache*/); | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 268 | std::vector<audio_io_handle_t> outputsToReopen; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 269 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 270 | const DeviceVector activeMediaDevices = | 
|  | 271 | mEngine->getActiveMediaDevices(mAvailableOutputDevices); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 272 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 273 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jaideep Sharma | 89b5b85 | 2020-11-23 16:41:33 +0530 | [diff] [blame] | 274 | if (desc->isActive() && ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || | 
|  | 275 | (desc != mPrimaryOutput))) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 276 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 277 | // do not force device change on duplicated output because if device is 0, it will | 
|  | 278 | // also force a device 0 for the two outputs it is duplicated to which may override | 
|  | 279 | // a valid device selection on those outputs. | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 280 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 281 | && !desc->isDuplicated() | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 282 | && (!device_distinguishes_on_address(device->type()) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 283 | // always force when disconnecting (a non-duplicated device) | 
|  | 284 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 285 | setOutputDevices(desc, newDevices, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 286 | } | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 287 | if (!desc->isDuplicated() && desc->mProfile->hasDynamicAudioProfile() && | 
| jiabin | 498d215 | 2021-04-02 00:26:46 +0000 | [diff] [blame] | 288 | !activeMediaDevices.empty() && desc->devices() != activeMediaDevices && | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 289 | desc->supportsDevicesForPlayback(activeMediaDevices)) { | 
|  | 290 | // Reopen the output to query the dynamic profiles when there is not active | 
|  | 291 | // clients or all active clients will be rerouted. Otherwise, set the flag | 
|  | 292 | // `mPendingReopenToQueryProfiles` in the SwOutputDescriptor so that the output | 
|  | 293 | // can be reopened to query dynamic profiles when all clients are inactive. | 
|  | 294 | if (areAllActiveTracksRerouted(desc)) { | 
|  | 295 | outputsToReopen.push_back(mOutputs.keyAt(i)); | 
|  | 296 | } else { | 
|  | 297 | desc->mPendingReopenToQueryProfiles = true; | 
|  | 298 | } | 
|  | 299 | } | 
|  | 300 | if (!desc->supportsDevicesForPlayback(activeMediaDevices)) { | 
|  | 301 | // Clear the flag that previously set for re-querying profiles. | 
|  | 302 | desc->mPendingReopenToQueryProfiles = false; | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 | for (const auto& output : outputsToReopen) { | 
|  | 306 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); | 
|  | 307 | closeOutput(output); | 
|  | 308 | openOutputWithProfileAndDevice(desc->mProfile, activeMediaDevices); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 309 | } | 
|  | 310 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 311 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 312 | cleanUpForDevice(device); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 313 | } | 
|  | 314 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 315 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 316 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 317 | }  // end if is output device | 
|  | 318 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 319 | // handle input devices | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 320 | if (audio_is_input_device(device->type())) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 321 | ssize_t index = mAvailableInputDevices.indexOf(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 322 | switch (state) | 
|  | 323 | { | 
|  | 324 | // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 325 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 326 | if (index >= 0) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 327 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 328 | return INVALID_OPERATION; | 
|  | 329 | } | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 330 |  | 
|  | 331 | if (mAvailableInputDevices.add(device) < 0) { | 
|  | 332 | return NO_MEMORY; | 
|  | 333 | } | 
|  | 334 |  | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 335 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic | 
|  | 336 | // parameters on newly connected devices (instead of opening the inputs...) | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 337 | broadcastDeviceConnectionState(device, state); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 338 |  | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 339 | if (checkInputsForDevice(device, state) != NO_ERROR) { | 
|  | 340 | mAvailableInputDevices.remove(device); | 
|  | 341 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 342 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 343 |  | 
|  | 344 | mHwModules.cleanUpForDevice(device); | 
|  | 345 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 346 | return INVALID_OPERATION; | 
|  | 347 | } | 
|  | 348 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 349 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 350 |  | 
|  | 351 | // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 352 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 353 | if (index < 0) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 354 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 355 | return INVALID_OPERATION; | 
|  | 356 | } | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 357 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 358 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 359 |  | 
|  | 360 | // Set Disconnect to HALs | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 361 | broadcastDeviceConnectionState(device, state); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 362 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 363 | mAvailableInputDevices.remove(device); | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 364 |  | 
|  | 365 | checkInputsForDevice(device, state); | 
| Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 366 |  | 
|  | 367 | // remove device from mReportedFormatsMap cache | 
|  | 368 | mReportedFormatsMap.erase(device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 369 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 370 |  | 
|  | 371 | default: | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 372 | ALOGE("%s() invalid state: %x", __func__, state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 373 | return BAD_VALUE; | 
|  | 374 | } | 
|  | 375 |  | 
| Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 376 | // Propagate device availability to Engine | 
|  | 377 | setEngineDeviceConnectionState(device, state); | 
|  | 378 |  | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 379 | checkCloseInputs(); | 
| Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 380 | // As the input device list can impact the output device selection, update | 
|  | 381 | // getDeviceForStrategy() cache | 
|  | 382 | updateDevicesAndOutputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 383 |  | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 384 | (void)updateCallRouting(false /*fromCache*/); | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 385 | // Reconnect Audio Source | 
|  | 386 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { | 
|  | 387 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); | 
|  | 388 | checkAudioSourceForAttributes(attributes); | 
|  | 389 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 390 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 391 | cleanUpForDevice(device); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 392 | } | 
|  | 393 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 394 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 395 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 396 | } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 397 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 398 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 399 | return BAD_VALUE; | 
|  | 400 | } | 
|  | 401 |  | 
| Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 402 | void AudioPolicyManager::setEngineDeviceConnectionState(const sp<DeviceDescriptor> device, | 
|  | 403 | audio_policy_dev_state_t state) { | 
|  | 404 |  | 
|  | 405 | // the Engine does not have to know about remote submix devices used by dynamic audio policies | 
|  | 406 | if (audio_is_remote_submix_device(device->type()) && device->address() != "0") { | 
|  | 407 | return; | 
|  | 408 | } | 
|  | 409 | mEngine->setDeviceConnectionState(device, state); | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 413 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 414 | const char *device_address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 415 | { | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 416 | sp<DeviceDescriptor> devDesc = | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 417 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, | 
|  | 418 | false /* allowToCreate */, | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 419 | (strlen(device_address) != 0)/*matchAddress*/); | 
|  | 420 |  | 
|  | 421 | if (devDesc == 0) { | 
| François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 422 | ALOGV("getDeviceConnectionState() undeclared device, type %08x, address: %s", | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 423 | device, device_address); | 
|  | 424 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
|  | 425 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 426 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 427 | DeviceVector *deviceVector; | 
|  | 428 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 429 | if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 430 | deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 431 | } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 432 | deviceVector = &mAvailableInputDevices; | 
|  | 433 | } else { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 434 | ALOGW("%s() invalid device type %08x", __func__, device); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 435 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 436 | } | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 437 |  | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 438 | return (deviceVector->getDevice( | 
|  | 439 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 440 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 441 | } | 
|  | 442 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 443 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, | 
|  | 444 | const char *device_address, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 445 | const char *device_name, | 
|  | 446 | audio_format_t encodedFormat) | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 447 | { | 
|  | 448 | status_t status; | 
| Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 449 | String8 reply; | 
|  | 450 | AudioParameter param; | 
|  | 451 | int isReconfigA2dpSupported = 0; | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 452 |  | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 453 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", | 
|  | 454 | device, device_address, device_name, encodedFormat); | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 455 |  | 
| Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 456 | // connect/disconnect only 1 device at a time | 
|  | 457 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 458 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 459 | // Check if the device is currently connected | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 460 | DeviceVector deviceList = mAvailableOutputDevices.getDevicesFromType(device); | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 461 | if (deviceList.empty()) { | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 462 | // Nothing to do: device is not connected | 
|  | 463 | return NO_ERROR; | 
|  | 464 | } | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 465 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 466 |  | 
| Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 467 | // For offloaded A2DP, Hw modules may have the capability to | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 468 | // configure codecs. | 
|  | 469 | // Handle two specific cases by sending a set parameter to | 
|  | 470 | // configure A2DP codecs. No need to toggle device state. | 
|  | 471 | // Case 1: A2DP active device switches from primary to primary | 
|  | 472 | // module | 
|  | 473 | // Case 2: A2DP device config changes on primary module. | 
| Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 474 | if (audio_is_a2dp_out_device(device) && hasPrimaryOutput()) { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 475 | sp<HwModule> module = mHwModules.getModuleForDeviceType(device, encodedFormat); | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 476 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); | 
|  | 477 | if (availablePrimaryOutputDevices().contains(devDesc) && | 
|  | 478 | (module != 0 && module->getHandle() == primaryHandle)) { | 
|  | 479 | reply = mpClientInterface->getParameters( | 
|  | 480 | AUDIO_IO_HANDLE_NONE, | 
|  | 481 | String8(AudioParameter::keyReconfigA2dpSupported)); | 
|  | 482 | AudioParameter repliedParameters(reply); | 
|  | 483 | repliedParameters.getInt( | 
|  | 484 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); | 
|  | 485 | if (isReconfigA2dpSupported) { | 
|  | 486 | const String8 key(AudioParameter::keyReconfigA2dp); | 
|  | 487 | param.add(key, String8("true")); | 
|  | 488 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 489 | devDesc->setEncodedFormat(encodedFormat); | 
|  | 490 | return NO_ERROR; | 
|  | 491 | } | 
| Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 492 | } | 
|  | 493 | } | 
| cnx421 | bd2dcc4 | 2020-07-11 14:58:44 +0800 | [diff] [blame] | 494 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); | 
|  | 495 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 496 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 497 | // mute media strategies and delay device switch by the largest | 
|  | 498 | // This avoid sending the music tail into the earpiece or headset. | 
|  | 499 | setStrategyMute(musicStrategy, true, desc); | 
|  | 500 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, | 
|  | 501 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), | 
|  | 502 | nullptr, true /*fromCache*/).types()); | 
|  | 503 | } | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 504 | // Toggle the device state: UNAVAILABLE -> AVAILABLE | 
|  | 505 | // This will force reading again the device configuration | 
|  | 506 | status = setDeviceConnectionState(device, | 
|  | 507 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 508 | device_address, device_name, | 
|  | 509 | devDesc->getEncodedFormat()); | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 510 | if (status != NO_ERROR) { | 
|  | 511 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", | 
|  | 512 | status); | 
|  | 513 | return status; | 
|  | 514 | } | 
|  | 515 |  | 
|  | 516 | status = setDeviceConnectionState(device, | 
|  | 517 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 518 | device_address, device_name, encodedFormat); | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 519 | if (status != NO_ERROR) { | 
|  | 520 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", | 
|  | 521 | status); | 
|  | 522 | return status; | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | return NO_ERROR; | 
|  | 526 | } | 
|  | 527 |  | 
| Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 528 | status_t AudioPolicyManager::getHwOffloadEncodingFormatsSupportedForA2DP( | 
|  | 529 | std::vector<audio_format_t> *formats) | 
|  | 530 | { | 
|  | 531 | ALOGV("getHwOffloadEncodingFormatsSupportedForA2DP()"); | 
| Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 532 | status_t status = NO_ERROR; | 
| Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 533 | std::unordered_set<audio_format_t> formatSet; | 
|  | 534 | sp<HwModule> primaryModule = | 
|  | 535 | mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY); | 
| Aniket Kumar Lata | fedb598 | 2019-07-03 11:20:36 -0700 | [diff] [blame] | 536 | if (primaryModule == nullptr) { | 
|  | 537 | ALOGE("%s() unable to get primary module", __func__); | 
|  | 538 | return NO_INIT; | 
|  | 539 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 540 | DeviceVector declaredDevices = primaryModule->getDeclaredDevices().getDevicesFromTypes( | 
|  | 541 | getAudioDeviceOutAllA2dpSet()); | 
| Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 542 | for (const auto& device : declaredDevices) { | 
|  | 543 | formatSet.insert(device->encodedFormats().begin(), device->encodedFormats().end()); | 
| Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 544 | } | 
| Aniket Kumar Lata | 89e8223 | 2019-01-19 18:14:10 -0800 | [diff] [blame] | 545 | formats->assign(formatSet.begin(), formatSet.end()); | 
| Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame] | 546 | return status; | 
|  | 547 | } | 
|  | 548 |  | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 549 | DeviceVector AudioPolicyManager::selectBestRxSinkDevicesForCall(bool fromCache) | 
|  | 550 | { | 
|  | 551 | DeviceVector rxSinkdevices{}; | 
|  | 552 | rxSinkdevices = mEngine->getOutputDevicesForAttributes( | 
|  | 553 | attributes_initializer(AUDIO_USAGE_VOICE_COMMUNICATION), nullptr, fromCache); | 
|  | 554 | if (!rxSinkdevices.isEmpty() && mAvailableOutputDevices.contains(rxSinkdevices.itemAt(0))) { | 
|  | 555 | auto rxSinkDevice = rxSinkdevices.itemAt(0); | 
|  | 556 | auto telephonyRxModule = mHwModules.getModuleForDeviceType( | 
|  | 557 | AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); | 
|  | 558 | // retrieve Rx Source device descriptor | 
|  | 559 | sp<DeviceDescriptor> rxSourceDevice = mAvailableInputDevices.getDevice( | 
|  | 560 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT); | 
|  | 561 |  | 
|  | 562 | // RX Telephony and Rx sink devices are declared by Primary Audio HAL | 
|  | 563 | if (isPrimaryModule(telephonyRxModule) && (telephonyRxModule->getHalVersionMajor() >= 3) && | 
|  | 564 | telephonyRxModule->supportsPatch(rxSourceDevice, rxSinkDevice)) { | 
|  | 565 | ALOGW("%s() device %s using HW Bridge", __func__, rxSinkDevice->toString().c_str()); | 
|  | 566 | return DeviceVector(rxSinkDevice); | 
|  | 567 | } | 
|  | 568 | } | 
|  | 569 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, | 
|  | 570 | // the device returned is not necessarily reachable via this output | 
|  | 571 | // (filter later by setOutputDevices()) | 
|  | 572 | return getNewOutputDevices(mPrimaryOutput, fromCache); | 
|  | 573 | } | 
|  | 574 |  | 
|  | 575 | status_t AudioPolicyManager::updateCallRouting(bool fromCache, uint32_t delayMs, uint32_t *waitMs) | 
|  | 576 | { | 
|  | 577 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
|  | 578 | DeviceVector rxDevices = selectBestRxSinkDevicesForCall(fromCache); | 
|  | 579 | return updateCallRoutingInternal(rxDevices, delayMs, waitMs); | 
|  | 580 | } | 
|  | 581 | return INVALID_OPERATION; | 
|  | 582 | } | 
|  | 583 |  | 
|  | 584 | status_t AudioPolicyManager::updateCallRoutingInternal( | 
|  | 585 | const DeviceVector &rxDevices, uint32_t delayMs, uint32_t *waitMs) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 586 | { | 
|  | 587 | bool createTxPatch = false; | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 588 | bool createRxPatch = false; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 589 | uint32_t muteWaitMs = 0; | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 590 | if(!hasPrimaryOutput() || | 
|  | 591 | mPrimaryOutput->devices().onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_STUB)) { | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 592 | return INVALID_OPERATION; | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 593 | } | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 594 | ALOG_ASSERT(!rxDevices.isEmpty(), "%s() no selected output device", __func__); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 595 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 596 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 597 | auto txSourceDevice = mEngine->getInputDeviceForAttributes(attr); | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 598 | ALOG_ASSERT(txSourceDevice != 0, "%s() input selected device not available", __func__); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 599 |  | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 600 | ALOGV("%s device rxDevice %s txDevice %s", __func__, | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 601 | rxDevices.itemAt(0)->toString().c_str(), txSourceDevice->toString().c_str()); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 602 |  | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 603 | disconnectTelephonyRxAudioSource(); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 604 | // release TX patch if any | 
|  | 605 | if (mCallTxPatch != 0) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 606 | releaseAudioPatchInternal(mCallTxPatch->getHandle()); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 607 | mCallTxPatch.clear(); | 
|  | 608 | } | 
|  | 609 |  | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 610 | auto telephonyRxModule = | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 611 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_IN_TELEPHONY_RX, AUDIO_FORMAT_DEFAULT); | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 612 | auto telephonyTxModule = | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 613 | mHwModules.getModuleForDeviceType(AUDIO_DEVICE_OUT_TELEPHONY_TX, AUDIO_FORMAT_DEFAULT); | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 614 | // retrieve Rx Source and Tx Sink device descriptors | 
|  | 615 | sp<DeviceDescriptor> rxSourceDevice = | 
|  | 616 | mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX, | 
|  | 617 | String8(), | 
|  | 618 | AUDIO_FORMAT_DEFAULT); | 
|  | 619 | sp<DeviceDescriptor> txSinkDevice = | 
|  | 620 | mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, | 
|  | 621 | String8(), | 
|  | 622 | AUDIO_FORMAT_DEFAULT); | 
|  | 623 |  | 
|  | 624 | // RX and TX Telephony device are declared by Primary Audio HAL | 
|  | 625 | if (isPrimaryModule(telephonyRxModule) && isPrimaryModule(telephonyTxModule) && | 
|  | 626 | (telephonyRxModule->getHalVersionMajor() >= 3)) { | 
|  | 627 | if (rxSourceDevice == 0 || txSinkDevice == 0) { | 
|  | 628 | // RX / TX Telephony device(s) is(are) not currently available | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 629 | ALOGE("%s() no telephony Tx and/or RX device", __func__); | 
|  | 630 | return INVALID_OPERATION; | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 631 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 632 | // createAudioPatchInternal now supports both HW / SW bridging | 
|  | 633 | createRxPatch = true; | 
|  | 634 | createTxPatch = true; | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 635 | } else { | 
|  | 636 | // If the RX device is on the primary HW module, then use legacy routing method for | 
|  | 637 | // voice calls via setOutputDevice() on primary output. | 
|  | 638 | // Otherwise, create two audio patches for TX and RX path. | 
|  | 639 | createRxPatch = !(availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) && | 
|  | 640 | (rxSourceDevice != 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 641 | // If the TX device is also on the primary HW module, setOutputDevice() will take care | 
|  | 642 | // of it due to legacy implementation. If not, create a patch. | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 643 | createTxPatch = !(availablePrimaryModuleInputDevices().contains(txSourceDevice)) && | 
|  | 644 | (txSinkDevice != 0); | 
|  | 645 | } | 
|  | 646 | // Use legacy routing method for voice calls via setOutputDevice() on primary output. | 
|  | 647 | // Otherwise, create two audio patches for TX and RX path. | 
|  | 648 | if (!createRxPatch) { | 
|  | 649 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 650 | } else { // create RX path audio patch | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 651 | connectTelephonyRxAudioSource(); | 
| juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 652 | // If the TX device is on the primary HW module but RX device is | 
|  | 653 | // on other HW module, SinkMetaData of telephony input should handle it | 
|  | 654 | // assuming the device uses audio HAL V5.0 and above | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 655 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 656 | if (createTxPatch) { // create TX path audio patch | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 657 | // terminate active capture if on the same HW module as the call TX source device | 
|  | 658 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 659 | // call TX device but this information is not in the audio patch and logic here must be | 
|  | 660 | // symmetric to the one in startInput() | 
|  | 661 | for (const auto& activeDesc : mInputs.getActiveInputs()) { | 
|  | 662 | if (activeDesc->hasSameHwModuleAs(txSourceDevice)) { | 
|  | 663 | closeActiveClients(activeDesc); | 
|  | 664 | } | 
|  | 665 | } | 
| François Gaffie | 9eb1855 | 2018-11-05 10:33:26 +0100 | [diff] [blame] | 666 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txSourceDevice, delayMs); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 667 | } | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 668 | if (waitMs != nullptr) { | 
|  | 669 | *waitMs = muteWaitMs; | 
|  | 670 | } | 
|  | 671 | return NO_ERROR; | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 672 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 673 |  | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 674 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 675 | bool isRx, const sp<DeviceDescriptor> &device, uint32_t delayMs) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 676 | PatchBuilder patchBuilder; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 677 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 678 | if (device == nullptr) { | 
|  | 679 | return nullptr; | 
|  | 680 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 681 |  | 
|  | 682 | // @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] | 683 | if (isRx) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 684 | patchBuilder.addSink(device). | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 685 | addSource(mAvailableInputDevices.getDevice( | 
|  | 686 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT)); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 687 | } else { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 688 | patchBuilder.addSource(device). | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 689 | addSink(mAvailableOutputDevices.getDevice( | 
|  | 690 | AUDIO_DEVICE_OUT_TELEPHONY_TX, String8(), AUDIO_FORMAT_DEFAULT)); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 691 | } | 
|  | 692 |  | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 693 | audio_patch_handle_t patchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 694 | status_t status = | 
|  | 695 | createAudioPatchInternal(patchBuilder.patch(), &patchHandle, mUidCached, delayMs); | 
|  | 696 | ssize_t index = mAudioPatches.indexOfKey(patchHandle); | 
|  | 697 | if (status != NO_ERROR || index < 0) { | 
|  | 698 | ALOGW("%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); | 
|  | 699 | return nullptr; | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 700 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 701 | return mAudioPatches.valueAt(index); | 
| Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 702 | } | 
|  | 703 |  | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 704 | bool AudioPolicyManager::isDeviceOfModule( | 
|  | 705 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { | 
|  | 706 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); | 
|  | 707 | if (module != 0) { | 
|  | 708 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) | 
|  | 709 | .indexOf(devDesc) != NAME_NOT_FOUND | 
|  | 710 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) | 
|  | 711 | .indexOf(devDesc) != NAME_NOT_FOUND; | 
|  | 712 | } | 
|  | 713 | return false; | 
|  | 714 | } | 
|  | 715 |  | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 716 | void AudioPolicyManager::connectTelephonyRxAudioSource() | 
|  | 717 | { | 
|  | 718 | disconnectTelephonyRxAudioSource(); | 
|  | 719 | const struct audio_port_config source = { | 
|  | 720 | .role = AUDIO_PORT_ROLE_SOURCE, .type = AUDIO_PORT_TYPE_DEVICE, | 
|  | 721 | .ext.device.type = AUDIO_DEVICE_IN_TELEPHONY_RX, .ext.device.address = "" | 
|  | 722 | }; | 
|  | 723 | const auto aa = mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL); | 
|  | 724 | status_t status = startAudioSource(&source, &aa, &mCallRxSourceClientPort, 0/*uid*/); | 
|  | 725 | ALOGE_IF(status != NO_ERROR, "%s failed to start Telephony Rx AudioSource", __func__); | 
|  | 726 | } | 
|  | 727 |  | 
|  | 728 | void AudioPolicyManager::disconnectTelephonyRxAudioSource() | 
|  | 729 | { | 
|  | 730 | stopAudioSource(mCallRxSourceClientPort); | 
|  | 731 | mCallRxSourceClientPort = AUDIO_PORT_HANDLE_NONE; | 
|  | 732 | } | 
|  | 733 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 734 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 735 | { | 
|  | 736 | ALOGV("setPhoneState() state %d", state); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 737 | // store previous phone state for management of sonification strategy below | 
|  | 738 | int oldState = mEngine->getPhoneState(); | 
|  | 739 |  | 
|  | 740 | if (mEngine->setPhoneState(state) != NO_ERROR) { | 
|  | 741 | ALOGW("setPhoneState() invalid or same state %d", state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 742 | return; | 
|  | 743 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 744 | /// Opens: can these line be executed after the switch of volume curves??? | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 745 | if (isStateInCall(oldState)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 746 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 747 | // force reevaluating accessibility routing when call stops | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 748 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 749 | } | 
|  | 750 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 751 | /** | 
|  | 752 | * Switching to or from incall state or switching between telephony and VoIP lead to force | 
|  | 753 | * routing command. | 
|  | 754 | */ | 
| Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 755 | bool force = ((isStateInCall(oldState) != isStateInCall(state)) | 
|  | 756 | || (isStateInCall(state) && (state != oldState))); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 757 |  | 
|  | 758 | // check for device and output changes triggered by new phone state | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 759 | checkForDeviceAndOutputChanges(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 760 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 761 | int delayMs = 0; | 
|  | 762 | if (isStateInCall(state)) { | 
|  | 763 | nsecs_t sysTime = systemTime(); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 764 | auto musicStrategy = streamToStrategy(AUDIO_STREAM_MUSIC); | 
|  | 765 | auto sonificationStrategy = streamToStrategy(AUDIO_STREAM_ALARM); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 766 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 767 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 768 | // mute media and sonification strategies and delay device switch by the largest | 
|  | 769 | // latency of any output where either strategy is active. | 
|  | 770 | // 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] | 771 | if ((desc->isStrategyActive(musicStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, sysTime) || | 
|  | 772 | desc->isStrategyActive(sonificationStrategy, SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 773 | sysTime)) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 774 | (delayMs < (int)desc->latency()*2)) { | 
|  | 775 | delayMs = desc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 776 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 777 | setStrategyMute(musicStrategy, true, desc); | 
|  | 778 | setStrategyMute(musicStrategy, false, desc, MUTE_TIME_MS, | 
|  | 779 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), | 
|  | 780 | nullptr, true /*fromCache*/).types()); | 
|  | 781 | setStrategyMute(sonificationStrategy, true, desc); | 
|  | 782 | setStrategyMute(sonificationStrategy, false, desc, MUTE_TIME_MS, | 
|  | 783 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_ALARM), | 
|  | 784 | nullptr, true /*fromCache*/).types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 785 | } | 
|  | 786 | } | 
|  | 787 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 788 | if (hasPrimaryOutput()) { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 789 | if (state == AUDIO_MODE_IN_CALL) { | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 790 | (void)updateCallRouting(false /*fromCache*/, delayMs); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 791 | } else { | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 792 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); | 
|  | 793 | // force routing command to audio hardware when ending call | 
|  | 794 | // even if no device change is needed | 
|  | 795 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { | 
|  | 796 | rxDevices = mPrimaryOutput->devices(); | 
|  | 797 | } | 
|  | 798 | if (oldState == AUDIO_MODE_IN_CALL) { | 
|  | 799 | disconnectTelephonyRxAudioSource(); | 
|  | 800 | if (mCallTxPatch != 0) { | 
|  | 801 | releaseAudioPatchInternal(mCallTxPatch->getHandle()); | 
|  | 802 | mCallTxPatch.clear(); | 
|  | 803 | } | 
|  | 804 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 805 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 806 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 807 | } | 
| Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 808 |  | 
|  | 809 | // reevaluate routing on all outputs in case tracks have been started during the call | 
|  | 810 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 811 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 812 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); | 
| Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 813 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 814 | setOutputDevices(desc, newDevices, !newDevices.isEmpty(), 0 /*delayMs*/); | 
| Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 815 | } | 
|  | 816 | } | 
|  | 817 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 818 | if (isStateInCall(state)) { | 
|  | 819 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 820 | // force reevaluating accessibility routing when call starts | 
|  | 821 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 822 | } | 
|  | 823 |  | 
|  | 824 | // 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] | 825 | mLimitRingtoneVolume = (state == AUDIO_MODE_RINGTONE && | 
|  | 826 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 827 | } | 
|  | 828 |  | 
| Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 829 | audio_mode_t AudioPolicyManager::getPhoneState() { | 
|  | 830 | return mEngine->getPhoneState(); | 
|  | 831 | } | 
|  | 832 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 833 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 834 | audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 835 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 836 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); | 
| Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 837 | if (config == mEngine->getForceUse(usage)) { | 
|  | 838 | return; | 
|  | 839 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 840 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 841 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { | 
|  | 842 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); | 
|  | 843 | return; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 844 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 845 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || | 
|  | 846 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || | 
|  | 847 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 848 |  | 
|  | 849 | // check for device and output changes triggered by new force usage | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 850 | checkForDeviceAndOutputChanges(); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 851 |  | 
| Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 852 | // force client reconnection to reevaluate flag AUDIO_FLAG_AUDIBILITY_ENFORCED | 
|  | 853 | if (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM) { | 
|  | 854 | mpClientInterface->invalidateStream(AUDIO_STREAM_SYSTEM); | 
|  | 855 | mpClientInterface->invalidateStream(AUDIO_STREAM_ENFORCED_AUDIBLE); | 
|  | 856 | } | 
|  | 857 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 858 | //FIXME: workaround for truncated touch sounds | 
|  | 859 | // to be removed when the problem is handled by system UI | 
|  | 860 | uint32_t delayMs = 0; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 861 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { | 
|  | 862 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; | 
|  | 863 | } | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 864 |  | 
|  | 865 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 866 | updateInputRouting(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 867 | } | 
|  | 868 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 869 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 870 | { | 
|  | 871 | ALOGV("setSystemProperty() property %s, value %s", property, value); | 
|  | 872 | } | 
|  | 873 |  | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 874 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict | 
|  | 875 | // search to profiles for direct outputs. | 
|  | 876 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 877 | const DeviceVector& devices, | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 878 | uint32_t samplingRate, | 
|  | 879 | audio_format_t format, | 
|  | 880 | audio_channel_mask_t channelMask, | 
|  | 881 | audio_output_flags_t flags, | 
|  | 882 | bool directOnly) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 883 | { | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 884 | if (directOnly) { | 
|  | 885 | // only retain flags that will drive the direct output profile selection | 
|  | 886 | // if explicitly requested | 
|  | 887 | static const uint32_t kRelevantFlags = | 
|  | 888 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | | 
| Aniket Kumar Lata | ba810b8 | 2019-07-03 11:15:33 -0700 | [diff] [blame] | 889 | AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 890 | flags = | 
|  | 891 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 892 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 893 |  | 
|  | 894 | sp<IOProfile> profile; | 
|  | 895 |  | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 896 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 897 | for (const auto& curProfile : hwModule->getOutputProfiles()) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 898 | if (!curProfile->isCompatibleProfile(devices, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 899 | samplingRate, NULL /*updatedSamplingRate*/, | 
|  | 900 | format, NULL /*updatedFormat*/, | 
|  | 901 | channelMask, NULL /*updatedChannelMask*/, | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 902 | flags)) { | 
|  | 903 | continue; | 
|  | 904 | } | 
|  | 905 | // reject profiles not corresponding to a device currently available | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 906 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 907 | continue; | 
|  | 908 | } | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 909 | // reject profiles if connected device does not support codec | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 910 | if (!curProfile->devicesSupportEncodedFormats(devices.types())) { | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 911 | continue; | 
|  | 912 | } | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 913 | if (!directOnly) return curProfile; | 
|  | 914 | // when searching for direct outputs, if several profiles are compatible, give priority | 
|  | 915 | // to one with offload capability | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 916 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 917 | continue; | 
|  | 918 | } | 
|  | 919 | profile = curProfile; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 920 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 921 | break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 922 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 923 | } | 
|  | 924 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 925 | return profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 926 | } | 
|  | 927 |  | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 928 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 929 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 930 | DeviceVector devices = mEngine->getOutputDevicesForStream(stream, false /*fromCache*/); | 
| Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 931 |  | 
|  | 932 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). | 
|  | 933 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, | 
|  | 934 | // format, flags, etc. This may result in some discrepancy for functions that utilize | 
|  | 935 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() | 
|  | 936 | // and AudioSystem::getOutputSamplingRate(). | 
|  | 937 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 938 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 939 | const audio_io_handle_t output = selectOutput(outputs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 940 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 941 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, | 
|  | 942 | devices.toString().c_str(), output); | 
| Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 943 | return output; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 944 | } | 
|  | 945 |  | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 946 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, | 
|  | 947 | const audio_attributes_t *srcAttr, | 
|  | 948 | audio_stream_type_t srcStream) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 949 | { | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 950 | if (srcAttr != NULL) { | 
|  | 951 | if (!isValidAttributes(srcAttr)) { | 
|  | 952 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", | 
|  | 953 | __func__, | 
|  | 954 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, | 
|  | 955 | srcAttr->tags); | 
|  | 956 | return BAD_VALUE; | 
|  | 957 | } | 
|  | 958 | *dstAttr = *srcAttr; | 
|  | 959 | } else { | 
|  | 960 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { | 
|  | 961 | ALOGE("%s:  invalid stream type", __func__); | 
|  | 962 | return BAD_VALUE; | 
|  | 963 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 964 | *dstAttr = mEngine->getAttributesForStreamType(srcStream); | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 965 | } | 
| Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 966 |  | 
|  | 967 | // Only honor audibility enforced when required. The client will be | 
|  | 968 | // forced to reconnect if the forced usage changes. | 
|  | 969 | 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] | 970 | dstAttr->flags = static_cast<audio_flags_mask_t>( | 
|  | 971 | dstAttr->flags & ~AUDIO_FLAG_AUDIBILITY_ENFORCED); | 
| Eric Laurent | 22fcda2 | 2019-05-17 16:28:47 -0700 | [diff] [blame] | 972 | } | 
|  | 973 |  | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 974 | return NO_ERROR; | 
|  | 975 | } | 
|  | 976 |  | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 977 | status_t AudioPolicyManager::getOutputForAttrInt( | 
|  | 978 | audio_attributes_t *resultAttr, | 
|  | 979 | audio_io_handle_t *output, | 
|  | 980 | audio_session_t session, | 
|  | 981 | const audio_attributes_t *attr, | 
|  | 982 | audio_stream_type_t *stream, | 
|  | 983 | uid_t uid, | 
|  | 984 | const audio_config_t *config, | 
|  | 985 | audio_output_flags_t *flags, | 
|  | 986 | audio_port_handle_t *selectedDeviceId, | 
|  | 987 | bool *isRequestedDeviceForExclusiveUse, | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 988 | std::vector<sp<AudioPolicyMix>> *secondaryMixes, | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 989 | output_type_t *outputType) | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 990 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 991 | DeviceVector outputDevices; | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 992 | const audio_port_handle_t requestedPortId = *selectedDeviceId; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 993 | DeviceVector msdDevices = getMsdAudioOutDevices(); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 994 | const sp<DeviceDescriptor> requestedDevice = | 
|  | 995 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); | 
|  | 996 |  | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 997 | *outputType = API_OUTPUT_INVALID; | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 998 | status_t status = getAudioAttributes(resultAttr, attr, *stream); | 
|  | 999 | if (status != NO_ERROR) { | 
|  | 1000 | return status; | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1001 | } | 
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1002 | if (auto it = mAllowedCapturePolicies.find(uid); it != end(mAllowedCapturePolicies)) { | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1003 | resultAttr->flags = static_cast<audio_flags_mask_t>(resultAttr->flags | it->second); | 
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 1004 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1005 | *stream = mEngine->getStreamTypeForAttributes(*resultAttr); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1006 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1007 | ALOGV("%s() attributes=%s stream=%s session %d selectedDeviceId %d", __func__, | 
|  | 1008 | toString(*resultAttr).c_str(), toString(*stream).c_str(), session, requestedPortId); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1009 |  | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1010 | // The primary output is the explicit routing (eg. setPreferredDevice) if specified, | 
|  | 1011 | //       otherwise, fallback to the dynamic policies, if none match, query the engine. | 
|  | 1012 | // 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] | 1013 | sp<AudioPolicyMix> primaryMix; | 
|  | 1014 | status = mPolicyMixes.getOutputForAttr(*resultAttr, uid, *flags, primaryMix, secondaryMixes); | 
| Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1015 | if (status != OK) { | 
|  | 1016 | return status; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1017 | } | 
| Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 1018 |  | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1019 | // Explicit routing is higher priority then any dynamic policy primary output | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1020 | bool usePrimaryOutputFromPolicyMixes = requestedDevice == nullptr && primaryMix != nullptr; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1021 |  | 
|  | 1022 | // FIXME: in case of RENDER policy, the output capabilities should be checked | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1023 | if ((usePrimaryOutputFromPolicyMixes | 
|  | 1024 | || (secondaryMixes != nullptr && !secondaryMixes->empty())) | 
| Kevin Rocard | c2afbdf | 2019-01-31 18:18:06 -0800 | [diff] [blame] | 1025 | && !audio_is_linear_pcm(config->format)) { | 
|  | 1026 | ALOGD("%s: rejecting request as dynamic audio policy only support pcm", __func__); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1027 | return BAD_VALUE; | 
|  | 1028 | } | 
|  | 1029 | if (usePrimaryOutputFromPolicyMixes) { | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1030 | sp<DeviceDescriptor> deviceDesc = | 
|  | 1031 | mAvailableOutputDevices.getDevice(primaryMix->mDeviceType, | 
|  | 1032 | primaryMix->mDeviceAddress, | 
|  | 1033 | AUDIO_FORMAT_DEFAULT); | 
|  | 1034 | sp<SwAudioOutputDescriptor> policyDesc = primaryMix->getOutput(); | 
| Eric Laurent | c64e0ab | 2020-04-30 15:59:34 -0700 | [diff] [blame] | 1035 | if (deviceDesc != nullptr | 
|  | 1036 | && (policyDesc == nullptr || (policyDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT))) { | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1037 | audio_io_handle_t newOutput; | 
|  | 1038 | status = openDirectOutput( | 
|  | 1039 | *stream, session, config, | 
|  | 1040 | (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT), | 
|  | 1041 | DeviceVector(deviceDesc), &newOutput); | 
|  | 1042 | if (status != NO_ERROR) { | 
|  | 1043 | policyDesc = nullptr; | 
|  | 1044 | } else { | 
|  | 1045 | policyDesc = mOutputs.valueFor(newOutput); | 
|  | 1046 | primaryMix->setOutput(policyDesc); | 
|  | 1047 | } | 
|  | 1048 | } | 
|  | 1049 | if (policyDesc != nullptr) { | 
|  | 1050 | policyDesc->mPolicyMix = primaryMix; | 
|  | 1051 | *output = policyDesc->mIoHandle; | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1052 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1053 |  | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1054 | ALOGV("getOutputForAttr() returns output %d", *output); | 
|  | 1055 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
|  | 1056 | *outputType = API_OUT_MIX_PLAYBACK; | 
|  | 1057 | } else { | 
|  | 1058 | *outputType = API_OUTPUT_LEGACY; | 
|  | 1059 | } | 
|  | 1060 | return NO_ERROR; | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1061 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1062 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1063 | // Virtual sources must always be dynamicaly or explicitly routed | 
|  | 1064 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
|  | 1065 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); | 
|  | 1066 | return BAD_VALUE; | 
|  | 1067 | } | 
|  | 1068 | // explicit routing managed by getDeviceForStrategy in APM is now handled by engine | 
|  | 1069 | // in order to let the choice of the order to future vendor engine | 
|  | 1070 | outputDevices = mEngine->getOutputDevicesForAttributes(*resultAttr, requestedDevice, false); | 
| Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 1071 |  | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1072 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1073 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1074 | } | 
|  | 1075 |  | 
| Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1076 | // Set incall music only if device was explicitly set, and fallback to the device which is | 
|  | 1077 | // chosen by the engine if not. | 
|  | 1078 | // FIXME: provide a more generic approach which is not device specific and move this back | 
|  | 1079 | // to getOutputForDevice. | 
| Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1080 | // 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] | 1081 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX) && | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1082 | (*stream == AUDIO_STREAM_MUSIC  || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && | 
| Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1083 | audio_is_linear_pcm(config->format) && | 
| Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 1084 | isCallAudioAccessible()) { | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1085 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { | 
| Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1086 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; | 
| François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1087 | *isRequestedDeviceForExclusiveUse = true; | 
| Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 1088 | } | 
|  | 1089 | } | 
|  | 1090 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1091 | ALOGV("%s() device %s, sampling rate %d, format %#x, channel mask %#x, flags %#x stream %s", | 
|  | 1092 | __func__, outputDevices.toString().c_str(), config->sample_rate, config->format, | 
|  | 1093 | config->channel_mask, *flags, toString(*stream).c_str()); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1094 |  | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1095 | *output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1096 | if (!msdDevices.isEmpty()) { | 
|  | 1097 | *output = getOutputForDevices(msdDevices, session, *stream, config, flags); | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1098 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdOutputPatches(&outputDevices) == NO_ERROR) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1099 | ALOGV("%s() Using MSD devices %s instead of devices %s", | 
|  | 1100 | __func__, msdDevices.toString().c_str(), outputDevices.toString().c_str()); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1101 | } else { | 
|  | 1102 | *output = AUDIO_IO_HANDLE_NONE; | 
|  | 1103 | } | 
|  | 1104 | } | 
|  | 1105 | if (*output == AUDIO_IO_HANDLE_NONE) { | 
| jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1106 | *output = getOutputForDevices(outputDevices, session, *stream, config, | 
| Eric Laurent | 4298441 | 2019-05-09 17:57:03 -0700 | [diff] [blame] | 1107 | flags, resultAttr->flags & AUDIO_FLAG_MUTE_HAPTIC); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1108 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1109 | if (*output == AUDIO_IO_HANDLE_NONE) { | 
|  | 1110 | return INVALID_OPERATION; | 
|  | 1111 | } | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1112 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1113 | *selectedDeviceId = getFirstDeviceId(outputDevices); | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1114 | for (auto &outputDevice : outputDevices) { | 
|  | 1115 | if (outputDevice->getId() == getConfig().getDefaultOutputDevice()->getId()) { | 
|  | 1116 | *selectedDeviceId = outputDevice->getId(); | 
|  | 1117 | break; | 
|  | 1118 | } | 
|  | 1119 | } | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1120 |  | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1121 | if (outputDevices.onlyContainsDevicesWithType(AUDIO_DEVICE_OUT_TELEPHONY_TX)) { | 
|  | 1122 | *outputType = API_OUTPUT_TELEPHONY_TX; | 
|  | 1123 | } else { | 
|  | 1124 | *outputType = API_OUTPUT_LEGACY; | 
|  | 1125 | } | 
|  | 1126 |  | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1127 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); | 
|  | 1128 |  | 
|  | 1129 | return NO_ERROR; | 
|  | 1130 | } | 
|  | 1131 |  | 
|  | 1132 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 1133 | audio_io_handle_t *output, | 
|  | 1134 | audio_session_t session, | 
|  | 1135 | audio_stream_type_t *stream, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1136 | const AttributionSourceState& attributionSource, | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1137 | const audio_config_t *config, | 
|  | 1138 | audio_output_flags_t *flags, | 
|  | 1139 | audio_port_handle_t *selectedDeviceId, | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1140 | audio_port_handle_t *portId, | 
| Eric Laurent | 8a1095a | 2019-11-08 14:44:16 -0800 | [diff] [blame] | 1141 | std::vector<audio_io_handle_t> *secondaryOutputs, | 
|  | 1142 | output_type_t *outputType) | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1143 | { | 
|  | 1144 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE | 
|  | 1145 | if (*portId != AUDIO_PORT_HANDLE_NONE) { | 
|  | 1146 | return INVALID_OPERATION; | 
|  | 1147 | } | 
| Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 1148 | const uid_t uid = VALUE_OR_RETURN_STATUS( | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 1149 | aidl2legacy_int32_t_uid_t(attributionSource.uid)); | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1150 | const audio_port_handle_t requestedPortId = *selectedDeviceId; | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1151 | audio_attributes_t resultAttr; | 
| François Gaffie | f579db5 | 2018-11-13 11:25:16 +0100 | [diff] [blame] | 1152 | bool isRequestedDeviceForExclusiveUse = false; | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1153 | std::vector<sp<AudioPolicyMix>> secondaryMixes; | 
| Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1154 | const sp<DeviceDescriptor> requestedDevice = | 
|  | 1155 | mAvailableOutputDevices.getDeviceFromId(requestedPortId); | 
|  | 1156 |  | 
|  | 1157 | // Prevent from storing invalid requested device id in clients | 
|  | 1158 | const audio_port_handle_t sanitizedRequestedPortId = | 
|  | 1159 | requestedDevice != nullptr ? requestedPortId : AUDIO_PORT_HANDLE_NONE; | 
|  | 1160 | *selectedDeviceId = sanitizedRequestedPortId; | 
|  | 1161 |  | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1162 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1163 | config, flags, selectedDeviceId, &isRequestedDeviceForExclusiveUse, | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1164 | secondaryOutputs != nullptr ? &secondaryMixes : nullptr, outputType); | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1165 | if (status != NO_ERROR) { | 
|  | 1166 | return status; | 
|  | 1167 | } | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1168 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryOutputDescs; | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1169 | if (secondaryOutputs != nullptr) { | 
|  | 1170 | for (auto &secondaryMix : secondaryMixes) { | 
|  | 1171 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); | 
|  | 1172 | if (outputDesc != nullptr && | 
|  | 1173 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { | 
|  | 1174 | secondaryOutputs->push_back(outputDesc->mIoHandle); | 
|  | 1175 | weakSecondaryOutputDescs.push_back(outputDesc); | 
|  | 1176 | } | 
|  | 1177 | } | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1178 | } | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1179 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1180 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, | 
| Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1181 | .channel_mask = config->channel_mask, | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1182 | .format = config->format, | 
| Nick Desaulniers | a30e320 | 2019-10-18 13:38:23 -0700 | [diff] [blame] | 1183 | }; | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1184 | *portId = PolicyAudioPort::getNextUniqueId(); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1185 |  | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1186 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1187 | sp<TrackClientDescriptor> clientDesc = | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1188 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, | 
| Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1189 | sanitizedRequestedPortId, *stream, | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1190 | mEngine->getProductStrategyForAttributes(resultAttr), | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1191 | toVolumeSource(resultAttr), | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1192 | *flags, isRequestedDeviceForExclusiveUse, | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 1193 | std::move(weakSecondaryOutputDescs), | 
|  | 1194 | outputDesc->mPolicyMix); | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1195 | outputDesc->addClient(clientDesc); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1196 |  | 
| Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 1197 | ALOGV("%s() returns output %d requestedPortId %d selectedDeviceId %d for port ID %d", __func__, | 
|  | 1198 | *output, requestedPortId, *selectedDeviceId, *portId); | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1199 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1200 | return NO_ERROR; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1201 | } | 
|  | 1202 |  | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1203 | status_t AudioPolicyManager::openDirectOutput(audio_stream_type_t stream, | 
|  | 1204 | audio_session_t session, | 
|  | 1205 | const audio_config_t *config, | 
|  | 1206 | audio_output_flags_t flags, | 
|  | 1207 | const DeviceVector &devices, | 
|  | 1208 | audio_io_handle_t *output) { | 
|  | 1209 |  | 
|  | 1210 | *output = AUDIO_IO_HANDLE_NONE; | 
|  | 1211 |  | 
|  | 1212 | // skip direct output selection if the request can obviously be attached to a mixed output | 
|  | 1213 | // and not explicitly requested | 
|  | 1214 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
|  | 1215 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && | 
|  | 1216 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { | 
|  | 1217 | return NAME_NOT_FOUND; | 
|  | 1218 | } | 
|  | 1219 |  | 
|  | 1220 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. | 
|  | 1221 | // This prevents creating an offloaded track and tearing it down immediately after start | 
|  | 1222 | // when audioflinger detects there is an active non offloadable effect. | 
|  | 1223 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 1224 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 1225 | // in the background. | 
|  | 1226 | sp<IOProfile> profile; | 
|  | 1227 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
|  | 1228 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { | 
|  | 1229 | profile = getProfileForOutput( | 
|  | 1230 | devices, config->sample_rate, config->format, config->channel_mask, | 
|  | 1231 | flags, true /* directOnly */); | 
|  | 1232 | } | 
|  | 1233 |  | 
|  | 1234 | if (profile == nullptr) { | 
|  | 1235 | return NAME_NOT_FOUND; | 
|  | 1236 | } | 
|  | 1237 |  | 
|  | 1238 | // exclusive outputs for MMAP and Offload are enforced by different session ids. | 
|  | 1239 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 1240 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 1241 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
|  | 1242 | // reuse direct output if currently open by the same client | 
|  | 1243 | // and configured with same parameters | 
|  | 1244 | if ((config->sample_rate == desc->getSamplingRate()) && | 
|  | 1245 | (config->format == desc->getFormat()) && | 
|  | 1246 | (config->channel_mask == desc->getChannelMask()) && | 
|  | 1247 | (session == desc->mDirectClientSession)) { | 
|  | 1248 | desc->mDirectOpenCount++; | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 1249 | ALOGV("%s reusing direct output %d for session %d", __func__, | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1250 | mOutputs.keyAt(i), session); | 
|  | 1251 | *output = mOutputs.keyAt(i); | 
|  | 1252 | return NO_ERROR; | 
|  | 1253 | } | 
|  | 1254 | } | 
|  | 1255 | } | 
|  | 1256 |  | 
|  | 1257 | if (!profile->canOpenNewIo()) { | 
|  | 1258 | return NAME_NOT_FOUND; | 
|  | 1259 | } | 
|  | 1260 |  | 
|  | 1261 | sp<SwAudioOutputDescriptor> outputDesc = | 
|  | 1262 | new SwAudioOutputDescriptor(profile, mpClientInterface); | 
|  | 1263 |  | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1264 | // An MSD patch may be using the only output stream that can service this request. Release | 
|  | 1265 | // all MSD patches to prioritize this request over any active output on MSD. | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1266 | releaseMsdOutputPatches(devices); | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1267 |  | 
|  | 1268 | status_t status = outputDesc->open(config, devices, stream, flags, output); | 
|  | 1269 |  | 
|  | 1270 | // only accept an output with the requested parameters | 
|  | 1271 | if (status != NO_ERROR || | 
|  | 1272 | (config->sample_rate != 0 && config->sample_rate != outputDesc->getSamplingRate()) || | 
|  | 1273 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->getFormat()) || | 
|  | 1274 | (config->channel_mask != 0 && config->channel_mask != outputDesc->getChannelMask())) { | 
|  | 1275 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," | 
|  | 1276 | "format %d %d, channel mask %04x %04x", __func__, *output, config->sample_rate, | 
|  | 1277 | outputDesc->getSamplingRate(), config->format, outputDesc->getFormat(), | 
|  | 1278 | config->channel_mask, outputDesc->getChannelMask()); | 
|  | 1279 | if (*output != AUDIO_IO_HANDLE_NONE) { | 
|  | 1280 | outputDesc->close(); | 
|  | 1281 | } | 
|  | 1282 | // fall back to mixer output if possible when the direct output could not be open | 
|  | 1283 | if (audio_is_linear_pcm(config->format) && | 
|  | 1284 | config->sample_rate  <= SAMPLE_RATE_HZ_MAX) { | 
|  | 1285 | return NAME_NOT_FOUND; | 
|  | 1286 | } | 
|  | 1287 | *output = AUDIO_IO_HANDLE_NONE; | 
|  | 1288 | return BAD_VALUE; | 
|  | 1289 | } | 
|  | 1290 | outputDesc->mDirectOpenCount = 1; | 
|  | 1291 | outputDesc->mDirectClientSession = session; | 
|  | 1292 |  | 
|  | 1293 | addOutput(*output, outputDesc); | 
|  | 1294 | mPreviousOutputs = mOutputs; | 
|  | 1295 | ALOGV("%s returns new direct output %d", __func__, *output); | 
|  | 1296 | mpClientInterface->onAudioPortListUpdate(); | 
|  | 1297 | return NO_ERROR; | 
|  | 1298 | } | 
|  | 1299 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1300 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( | 
|  | 1301 | const DeviceVector &devices, | 
| Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1302 | audio_session_t session, | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1303 | audio_stream_type_t stream, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1304 | const audio_config_t *config, | 
| jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1305 | audio_output_flags_t *flags, | 
|  | 1306 | bool forceMutingHaptic) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1307 | { | 
| Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1308 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1309 |  | 
| jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1310 | // Discard haptic channel mask when forcing muting haptic channels. | 
|  | 1311 | audio_channel_mask_t channelMask = forceMutingHaptic | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 1312 | ? static_cast<audio_channel_mask_t>(config->channel_mask & ~AUDIO_CHANNEL_HAPTIC_ALL) | 
|  | 1313 | : config->channel_mask; | 
| jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1314 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1315 | // open a direct output if required by specified parameters | 
|  | 1316 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 1317 | // and all common behaviors are driven by checking only the direct flag | 
|  | 1318 | // this should normally be set appropriately in the policy configuration file | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1319 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 1320 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1321 | } | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1322 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 1323 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1324 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1325 | // only allow deep buffering for music stream type | 
|  | 1326 | if (stream != AUDIO_STREAM_MUSIC) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1327 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1328 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1329 | *flags == AUDIO_OUTPUT_FLAG_NONE && | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1330 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { | 
|  | 1331 | // use DEEP_BUFFER as default output for music stream type | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1332 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1333 | } | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1334 | if (stream == AUDIO_STREAM_TTS) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1335 | *flags = AUDIO_OUTPUT_FLAG_TTS; | 
| Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1336 | } else if (stream == AUDIO_STREAM_VOICE_CALL && | 
| Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1337 | audio_is_linear_pcm(config->format) && | 
|  | 1338 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { | 
| Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1339 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | | 
| Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1340 | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 1341 | ALOGV("Set VoIP and Direct output flags for PCM format"); | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1342 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1343 |  | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 1344 | audio_config_t directConfig = *config; | 
|  | 1345 | directConfig.channel_mask = channelMask; | 
|  | 1346 | status_t status = openDirectOutput(stream, session, &directConfig, *flags, devices, &output); | 
|  | 1347 | if (status != NAME_NOT_FOUND) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1348 | return output; | 
|  | 1349 | } | 
|  | 1350 |  | 
| Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1351 | // A request for HW A/V sync cannot fallback to a mixed output because time | 
|  | 1352 | // stamps are embedded in audio data | 
| Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1353 | 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] | 1354 | return AUDIO_IO_HANDLE_NONE; | 
|  | 1355 | } | 
|  | 1356 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1357 | // ignoring channel mask due to downmix capability in mixer | 
|  | 1358 |  | 
|  | 1359 | // open a non direct output | 
|  | 1360 |  | 
|  | 1361 | // for non direct outputs, only PCM is supported | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1362 | if (audio_is_linear_pcm(config->format)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1363 | // get which output is suitable for the specified stream. The actual | 
|  | 1364 | // routing change will happen when startOutput() will be called | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1365 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1366 |  | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1367 | // 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] | 1368 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); | 
| jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1369 | output = selectOutput( | 
|  | 1370 | outputs, *flags, config->format, channelMask, config->sample_rate, session); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1371 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1372 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1373 | "sampling rate %d, format %#x, channels %#x, flags %#x", | 
| jiabin | e375d41 | 2019-02-26 12:54:53 -0800 | [diff] [blame] | 1374 | stream, config->sample_rate, config->format, channelMask, *flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1375 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1376 | return output; | 
|  | 1377 | } | 
|  | 1378 |  | 
| Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1379 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1380 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, | 
|  | 1381 | mAvailableInputDevices); | 
|  | 1382 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); | 
|  | 1383 | } | 
|  | 1384 |  | 
|  | 1385 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { | 
|  | 1386 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, | 
|  | 1387 | mAvailableOutputDevices); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1388 | } | 
|  | 1389 |  | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1390 | const AudioPatchCollection AudioPolicyManager::getMsdOutputPatches() const { | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1391 | AudioPatchCollection msdPatches; | 
| Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1392 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); | 
|  | 1393 | if (msdModule != 0) { | 
|  | 1394 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { | 
|  | 1395 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); | 
|  | 1396 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { | 
|  | 1397 | const struct audio_port_config *source = &patch->mPatch.sources[j]; | 
|  | 1398 | if (source->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 1399 | source->ext.device.hw_module == msdModule->getHandle()) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1400 | msdPatches.addAudioPatch(patch->getHandle(), patch); | 
| Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1401 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1402 | } | 
|  | 1403 | } | 
|  | 1404 | } | 
|  | 1405 | return msdPatches; | 
|  | 1406 | } | 
|  | 1407 |  | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1408 | status_t AudioPolicyManager::getMsdProfiles(bool hwAvSync, | 
|  | 1409 | const InputProfileCollection &inputProfiles, | 
|  | 1410 | const OutputProfileCollection &outputProfiles, | 
|  | 1411 | const sp<DeviceDescriptor> &sourceDevice, | 
|  | 1412 | const sp<DeviceDescriptor> &sinkDevice, | 
|  | 1413 | AudioProfileVector& sourceProfiles, | 
|  | 1414 | AudioProfileVector& sinkProfiles) const { | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1415 | if (inputProfiles.isEmpty()) { | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1416 | ALOGE("%s() no input profiles for source module", __func__); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1417 | return NO_INIT; | 
|  | 1418 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1419 | if (outputProfiles.isEmpty()) { | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1420 | ALOGE("%s() no output profiles for sink module", __func__); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1421 | return NO_INIT; | 
|  | 1422 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1423 | for (const auto &inProfile : inputProfiles) { | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1424 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0) && | 
|  | 1425 | inProfile->supportsDevice(sourceDevice)) { | 
|  | 1426 | appendAudioProfiles(sourceProfiles, inProfile->getAudioProfiles()); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1427 | } | 
|  | 1428 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1429 | for (const auto &outProfile : outputProfiles) { | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1430 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) && | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1431 | outProfile->supportsDevice(sinkDevice)) { | 
|  | 1432 | appendAudioProfiles(sinkProfiles, outProfile->getAudioProfiles()); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1433 | } | 
|  | 1434 | } | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1435 | return NO_ERROR; | 
|  | 1436 | } | 
|  | 1437 |  | 
|  | 1438 | status_t AudioPolicyManager::getBestMsdConfig(bool hwAvSync, | 
|  | 1439 | const AudioProfileVector &sourceProfiles, const AudioProfileVector &sinkProfiles, | 
|  | 1440 | audio_port_config *sourceConfig, audio_port_config *sinkConfig) const | 
|  | 1441 | { | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1442 | struct audio_config_base bestSinkConfig; | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1443 | status_t result = findBestMatchingOutputConfig(sourceProfiles, sinkProfiles, | 
|  | 1444 | msdCompressedFormatsOrder, msdSurroundChannelMasksOrder, | 
|  | 1445 | true /*preferHigherSamplingRates*/, bestSinkConfig); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1446 | if (result != NO_ERROR) { | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1447 | ALOGD("%s() no matching config found for sink, hwAvSync: %d", | 
|  | 1448 | __func__, hwAvSync); | 
| Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1449 | return result; | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1450 | } | 
|  | 1451 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; | 
|  | 1452 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; | 
|  | 1453 | sinkConfig->format = bestSinkConfig.format; | 
|  | 1454 | // For encoded streams force direct flag to prevent downstream mixing. | 
|  | 1455 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( | 
|  | 1456 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1457 | if (audio_is_iec61937_compatible(sinkConfig->format)) { | 
|  | 1458 | // For formats compatible with IEC61937 encapsulation, assume that | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1459 | // the input is IEC61937 framed (for proportional buffer sizing). | 
| Dean Wheatley | 5c9f083 | 2019-11-21 13:39:31 +1100 | [diff] [blame] | 1460 | // Add the AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO flag so downstream HAL can distinguish between | 
|  | 1461 | // raw and IEC61937 framed streams. | 
|  | 1462 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( | 
|  | 1463 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO); | 
|  | 1464 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1465 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; | 
|  | 1466 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. | 
|  | 1467 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); | 
|  | 1468 | sourceConfig->format = bestSinkConfig.format; | 
|  | 1469 | // Copy input stream directly without any processing (e.g. resampling). | 
|  | 1470 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( | 
|  | 1471 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); | 
|  | 1472 | if (hwAvSync) { | 
|  | 1473 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( | 
|  | 1474 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
|  | 1475 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( | 
|  | 1476 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); | 
|  | 1477 | } | 
|  | 1478 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | | 
|  | 1479 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; | 
|  | 1480 | sinkConfig->config_mask |= config_mask; | 
|  | 1481 | sourceConfig->config_mask |= config_mask; | 
|  | 1482 | return NO_ERROR; | 
|  | 1483 | } | 
|  | 1484 |  | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1485 | PatchBuilder AudioPolicyManager::buildMsdPatch(bool msdIsSource, | 
|  | 1486 | const sp<DeviceDescriptor> &device) const | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1487 | { | 
|  | 1488 | PatchBuilder patchBuilder; | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1489 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); | 
|  | 1490 | ALOG_ASSERT(msdModule != nullptr, "MSD module not available"); | 
|  | 1491 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); | 
|  | 1492 | if (deviceModule == nullptr) { | 
|  | 1493 | ALOGE("%s() unable to get module for %s", __func__, device->toString().c_str()); | 
|  | 1494 | return patchBuilder; | 
|  | 1495 | } | 
|  | 1496 | const InputProfileCollection inputProfiles = msdIsSource ? | 
|  | 1497 | msdModule->getInputProfiles() : deviceModule->getInputProfiles(); | 
|  | 1498 | const OutputProfileCollection outputProfiles = msdIsSource ? | 
|  | 1499 | deviceModule->getOutputProfiles() : msdModule->getOutputProfiles(); | 
|  | 1500 |  | 
|  | 1501 | const sp<DeviceDescriptor> sourceDevice = msdIsSource ? getMsdAudioInDevice() : device; | 
|  | 1502 | const sp<DeviceDescriptor> sinkDevice = msdIsSource ? | 
|  | 1503 | device : getMsdAudioOutDevices().itemAt(0); | 
|  | 1504 | patchBuilder.addSource(sourceDevice).addSink(sinkDevice); | 
|  | 1505 |  | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1506 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; | 
|  | 1507 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1508 | AudioProfileVector sourceProfiles; | 
|  | 1509 | AudioProfileVector sinkProfiles; | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1510 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. | 
|  | 1511 | // For now, we just forcefully try with HwAvSync first. | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1512 | for (auto hwAvSync : { true, false }) { | 
|  | 1513 | if (getMsdProfiles(hwAvSync, inputProfiles, outputProfiles, sourceDevice, sinkDevice, | 
|  | 1514 | sourceProfiles, sinkProfiles) != NO_ERROR) { | 
|  | 1515 | continue; | 
|  | 1516 | } | 
|  | 1517 | if (getBestMsdConfig(hwAvSync, sourceProfiles, sinkProfiles, &sourceConfig, | 
|  | 1518 | &sinkConfig) == NO_ERROR) { | 
|  | 1519 | // Found a matching config. Re-create PatchBuilder with this config. | 
|  | 1520 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); | 
|  | 1521 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1522 | } | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1523 | ALOGV("%s() no matching config found. Fall through to default PCM patch" | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1524 | " supporting PCM format conversion.", __func__); | 
|  | 1525 | return patchBuilder; | 
|  | 1526 | } | 
|  | 1527 |  | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1528 | status_t AudioPolicyManager::setMsdOutputPatches(const DeviceVector *outputDevices) { | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1529 | DeviceVector devices; | 
|  | 1530 | if (outputDevices != nullptr && outputDevices->size() > 0) { | 
|  | 1531 | devices.add(*outputDevices); | 
|  | 1532 | } else { | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1533 | // Use media strategy for unspecified output device. This should only | 
|  | 1534 | // occur on checkForDeviceAndOutputChanges(). Device connection events may | 
|  | 1535 | // therefore invalidate explicit routing requests. | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1536 | devices = mEngine->getOutputDevicesForAttributes( | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1537 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1538 | 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] | 1539 | } | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1540 | std::vector<PatchBuilder> patchesToCreate; | 
|  | 1541 | for (auto i = 0u; i < devices.size(); ++i) { | 
|  | 1542 | ALOGV("%s() for device %s", __func__, devices[i]->toString().c_str()); | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1543 | patchesToCreate.push_back(buildMsdPatch(true /*msdIsSource*/, devices[i])); | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1544 | } | 
|  | 1545 | // Retain only the MSD patches associated with outputDevices request. | 
|  | 1546 | // Tear down the others, and create new ones as needed. | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1547 | AudioPatchCollection patchesToRemove = getMsdOutputPatches(); | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1548 | for (auto it = patchesToCreate.begin(); it != patchesToCreate.end(); ) { | 
|  | 1549 | auto retainedPatch = false; | 
|  | 1550 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { | 
|  | 1551 | if (audio_patches_are_equal(it->patch(), &patchesToRemove[i]->mPatch)) { | 
|  | 1552 | patchesToRemove.removeItemsAt(i); | 
|  | 1553 | retainedPatch = true; | 
|  | 1554 | break; | 
|  | 1555 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1556 | } | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1557 | if (retainedPatch) { | 
|  | 1558 | it = patchesToCreate.erase(it); | 
|  | 1559 | continue; | 
|  | 1560 | } | 
|  | 1561 | ++it; | 
|  | 1562 | } | 
|  | 1563 | if (patchesToCreate.size() == 0 && patchesToRemove.size() == 0) { | 
|  | 1564 | return NO_ERROR; | 
|  | 1565 | } | 
|  | 1566 | for (auto i = 0u; i < patchesToRemove.size(); ++i) { | 
|  | 1567 | auto ¤tPatch = patchesToRemove.valueAt(i); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 1568 | releaseAudioPatch(currentPatch->getHandle(), mUidCached); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1569 | } | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1570 | status_t status = NO_ERROR; | 
|  | 1571 | for (const auto &p : patchesToCreate) { | 
|  | 1572 | auto currStatus = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, | 
|  | 1573 | p.patch(), 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); | 
|  | 1574 | char message[256]; | 
|  | 1575 | snprintf(message, sizeof(message), "%s() %s: creating MSD patch from device:IN_BUS to " | 
|  | 1576 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, | 
|  | 1577 | currStatus == NO_ERROR ? "Success" : "Error", | 
|  | 1578 | p.patch()->sinks[0].ext.device.type, p.patch()->sources[0].format, | 
|  | 1579 | p.patch()->sources[0].channel_mask, p.patch()->sources[0].sample_rate); | 
|  | 1580 | if (currStatus == NO_ERROR) { | 
|  | 1581 | ALOGD("%s", message); | 
|  | 1582 | } else { | 
|  | 1583 | ALOGE("%s", message); | 
|  | 1584 | if (status == NO_ERROR) { | 
|  | 1585 | status = currStatus; | 
|  | 1586 | } | 
|  | 1587 | } | 
|  | 1588 | } | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1589 | return status; | 
|  | 1590 | } | 
|  | 1591 |  | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 1592 | void AudioPolicyManager::releaseMsdOutputPatches(const DeviceVector& devices) { | 
|  | 1593 | AudioPatchCollection msdPatches = getMsdOutputPatches(); | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 1594 | for (size_t i = 0; i < msdPatches.size(); i++) { | 
|  | 1595 | const auto& patch = msdPatches[i]; | 
|  | 1596 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { | 
|  | 1597 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; | 
|  | 1598 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && devices.getDevice(sink->ext.device.type, | 
|  | 1599 | String8(sink->ext.device.address), AUDIO_FORMAT_DEFAULT) != nullptr) { | 
|  | 1600 | releaseAudioPatch(patch->getHandle(), mUidCached); | 
|  | 1601 | break; | 
|  | 1602 | } | 
|  | 1603 | } | 
|  | 1604 | } | 
|  | 1605 | } | 
|  | 1606 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1607 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, | 
| jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1608 | audio_output_flags_t flags, | 
|  | 1609 | audio_format_t format, | 
|  | 1610 | audio_channel_mask_t channelMask, | 
|  | 1611 | uint32_t samplingRate, | 
|  | 1612 | audio_session_t sessionId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1613 | { | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1614 | LOG_ALWAYS_FATAL_IF(!(format == AUDIO_FORMAT_INVALID || audio_is_linear_pcm(format)), | 
|  | 1615 | "%s called with format %#x", __func__, format); | 
|  | 1616 |  | 
| jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 1617 | // Return the output that haptic-generating attached to when 1) session id is specified, | 
|  | 1618 | // 2) haptic-generating effect exists for given session id and 3) the output that | 
|  | 1619 | // haptic-generating effect attached to is in given outputs. | 
|  | 1620 | if (sessionId != AUDIO_SESSION_NONE) { | 
|  | 1621 | audio_io_handle_t hapticGeneratingOutput = mEffects.getIoForSession( | 
|  | 1622 | sessionId, FX_IID_HAPTICGENERATOR); | 
|  | 1623 | if (outputs.indexOf(hapticGeneratingOutput) >= 0) { | 
|  | 1624 | return hapticGeneratingOutput; | 
|  | 1625 | } | 
|  | 1626 | } | 
|  | 1627 |  | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1628 | // Flags disqualifying an output: the match must happen before calling selectOutput() | 
|  | 1629 | static const audio_output_flags_t kExcludedFlags = (audio_output_flags_t) | 
|  | 1630 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 1631 |  | 
|  | 1632 | // Flags expressing a functional request: must be honored in priority over | 
|  | 1633 | // other criteria | 
|  | 1634 | static const audio_output_flags_t kFunctionalFlags = (audio_output_flags_t) | 
|  | 1635 | (AUDIO_OUTPUT_FLAG_VOIP_RX | AUDIO_OUTPUT_FLAG_INCALL_MUSIC | | 
|  | 1636 | AUDIO_OUTPUT_FLAG_TTS | AUDIO_OUTPUT_FLAG_DIRECT_PCM); | 
|  | 1637 | // Flags expressing a performance request: have lower priority than serving | 
|  | 1638 | // requested sampling rate or channel mask | 
|  | 1639 | static const audio_output_flags_t kPerformanceFlags = (audio_output_flags_t) | 
|  | 1640 | (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER | | 
|  | 1641 | AUDIO_OUTPUT_FLAG_RAW | AUDIO_OUTPUT_FLAG_SYNC); | 
|  | 1642 |  | 
|  | 1643 | const audio_output_flags_t functionalFlags = | 
|  | 1644 | (audio_output_flags_t)(flags & kFunctionalFlags); | 
|  | 1645 | const audio_output_flags_t performanceFlags = | 
|  | 1646 | (audio_output_flags_t)(flags & kPerformanceFlags); | 
|  | 1647 |  | 
|  | 1648 | audio_io_handle_t bestOutput = (outputs.size() == 0) ? AUDIO_IO_HANDLE_NONE : outputs[0]; | 
|  | 1649 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1650 | // 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] | 1651 | // devices (the list was previously build by getOutputsForDevices()). | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1652 | // The priority is as follows: | 
| jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1653 | // 1: the output supporting haptic playback when requesting haptic playback | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1654 | // 2: the output with the highest number of requested functional flags | 
|  | 1655 | // 3: the output supporting the exact channel mask | 
|  | 1656 | // 4: the output with a higher channel count than requested | 
|  | 1657 | // 5: the output with a higher sampling rate than requested | 
|  | 1658 | // 6: the output with the highest number of requested performance flags | 
|  | 1659 | // 7: the output with the bit depth the closest to the requested one | 
|  | 1660 | // 8: the primary output | 
|  | 1661 | // 9: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1662 |  | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1663 | // matching criteria values in priority order for best matching output so far | 
|  | 1664 | std::vector<uint32_t> bestMatchCriteria(8, 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1665 |  | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1666 | const uint32_t channelCount = audio_channel_count_from_out_mask(channelMask); | 
|  | 1667 | const uint32_t hapticChannelCount = audio_channel_count_from_out_mask( | 
|  | 1668 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); | 
| Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1669 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1670 | for (audio_io_handle_t output : outputs) { | 
|  | 1671 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1672 | // matching criteria values in priority order for current output | 
|  | 1673 | std::vector<uint32_t> currentMatchCriteria(8, 0); | 
| jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1674 |  | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1675 | if (outputDesc->isDuplicated()) { | 
|  | 1676 | continue; | 
|  | 1677 | } | 
|  | 1678 | if ((kExcludedFlags & outputDesc->mFlags) != 0) { | 
|  | 1679 | continue; | 
|  | 1680 | } | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1681 |  | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1682 | // If haptic channel is specified, use the haptic output if present. | 
|  | 1683 | // When using haptic output, same audio format and sample rate are required. | 
|  | 1684 | const uint32_t outputHapticChannelCount = audio_channel_count_from_out_mask( | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1685 | outputDesc->getChannelMask() & AUDIO_CHANNEL_HAPTIC_ALL); | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1686 | if ((hapticChannelCount == 0) != (outputHapticChannelCount == 0)) { | 
|  | 1687 | continue; | 
|  | 1688 | } | 
|  | 1689 | if (outputHapticChannelCount >= hapticChannelCount | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1690 | && format == outputDesc->getFormat() | 
|  | 1691 | && samplingRate == outputDesc->getSamplingRate()) { | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1692 | currentMatchCriteria[0] = outputHapticChannelCount; | 
|  | 1693 | } | 
|  | 1694 |  | 
|  | 1695 | // functional flags match | 
|  | 1696 | currentMatchCriteria[1] = popcount(outputDesc->mFlags & functionalFlags); | 
|  | 1697 |  | 
|  | 1698 | // channel mask and channel count match | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1699 | uint32_t outputChannelCount = audio_channel_count_from_out_mask( | 
|  | 1700 | outputDesc->getChannelMask()); | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1701 | if (channelMask != AUDIO_CHANNEL_NONE && channelCount > 2 && | 
|  | 1702 | channelCount <= outputChannelCount) { | 
|  | 1703 | if ((audio_channel_mask_get_representation(channelMask) == | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1704 | audio_channel_mask_get_representation(outputDesc->getChannelMask())) && | 
|  | 1705 | ((channelMask & outputDesc->getChannelMask()) == channelMask)) { | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1706 | currentMatchCriteria[2] = outputChannelCount; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1707 | } | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1708 | currentMatchCriteria[3] = outputChannelCount; | 
|  | 1709 | } | 
|  | 1710 |  | 
|  | 1711 | // sampling rate match | 
|  | 1712 | if (samplingRate > SAMPLE_RATE_HZ_DEFAULT && | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1713 | samplingRate <= outputDesc->getSamplingRate()) { | 
|  | 1714 | currentMatchCriteria[4] = outputDesc->getSamplingRate(); | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1715 | } | 
|  | 1716 |  | 
|  | 1717 | // performance flags match | 
|  | 1718 | currentMatchCriteria[5] = popcount(outputDesc->mFlags & performanceFlags); | 
|  | 1719 |  | 
|  | 1720 | // format match | 
|  | 1721 | if (format != AUDIO_FORMAT_INVALID) { | 
|  | 1722 | currentMatchCriteria[6] = | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 1723 | PolicyAudioPort::kFormatDistanceMax - | 
|  | 1724 | PolicyAudioPort::formatDistance(format, outputDesc->getFormat()); | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1725 | } | 
|  | 1726 |  | 
|  | 1727 | // primary output match | 
|  | 1728 | currentMatchCriteria[7] = outputDesc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY; | 
|  | 1729 |  | 
|  | 1730 | // compare match criteria by priority then value | 
|  | 1731 | if (std::lexicographical_compare(bestMatchCriteria.begin(), bestMatchCriteria.end(), | 
|  | 1732 | currentMatchCriteria.begin(), currentMatchCriteria.end())) { | 
|  | 1733 | bestMatchCriteria = currentMatchCriteria; | 
|  | 1734 | bestOutput = output; | 
|  | 1735 |  | 
|  | 1736 | std::stringstream result; | 
|  | 1737 | std::copy(bestMatchCriteria.begin(), bestMatchCriteria.end(), | 
|  | 1738 | std::ostream_iterator<int>(result, " ")); | 
|  | 1739 | ALOGV("%s new bestOutput %d criteria %s", | 
|  | 1740 | __func__, bestOutput, result.str().c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1741 | } | 
|  | 1742 | } | 
|  | 1743 |  | 
| Eric Laurent | 16c66dd | 2019-05-01 17:54:10 -0700 | [diff] [blame] | 1744 | return bestOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1745 | } | 
|  | 1746 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1747 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1748 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1749 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 1750 |  | 
|  | 1751 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); | 
|  | 1752 | if (outputDesc == 0) { | 
|  | 1753 | ALOGW("startOutput() no output for client %d", portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1754 | return BAD_VALUE; | 
|  | 1755 | } | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1756 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1757 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1758 | ALOGV("startOutput() output %d, stream %d, session %d", | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1759 | outputDesc->mIoHandle, client->stream(), client->session()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1760 |  | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1761 | status_t status = outputDesc->start(); | 
|  | 1762 | if (status != NO_ERROR) { | 
|  | 1763 | return status; | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1764 | } | 
|  | 1765 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1766 | uint32_t delayMs; | 
|  | 1767 | status = startSource(outputDesc, client, &delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1768 |  | 
|  | 1769 | if (status != NO_ERROR) { | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1770 | outputDesc->stop(); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1771 | return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1772 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1773 | if (delayMs != 0) { | 
|  | 1774 | usleep(delayMs * 1000); | 
|  | 1775 | } | 
|  | 1776 |  | 
|  | 1777 | return status; | 
|  | 1778 | } | 
|  | 1779 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1780 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, | 
|  | 1781 | const sp<TrackClientDescriptor>& client, | 
|  | 1782 | uint32_t *delayMs) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1783 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1784 | // cannot start playback of STREAM_TTS if any other output is being used | 
|  | 1785 | uint32_t beaconMuteLatency = 0; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1786 |  | 
|  | 1787 | *delayMs = 0; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1788 | audio_stream_type_t stream = client->stream(); | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1789 | auto clientVolSrc = client->volumeSource(); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1790 | auto clientStrategy = client->strategy(); | 
|  | 1791 | auto clientAttr = client->attributes(); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1792 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 1793 | ALOGV("\t found BEACON stream"); | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1794 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive( | 
| Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 1795 | toVolumeSource(AUDIO_STREAM_TTS) /*sourceToIgnore*/)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1796 | return INVALID_OPERATION; | 
|  | 1797 | } else { | 
|  | 1798 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); | 
|  | 1799 | } | 
|  | 1800 | } else { | 
|  | 1801 | // some playback other than beacon starts | 
|  | 1802 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); | 
|  | 1803 | } | 
|  | 1804 |  | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1805 | // 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] | 1806 | // check active before incrementing usage count | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1807 | bool force = !outputDesc->isActive() && | 
|  | 1808 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1809 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1810 | DeviceVector devices; | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 1811 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1812 | const char *address = NULL; | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1813 | if (policyMix != nullptr) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1814 | audio_devices_t newDeviceType; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1815 | address = policyMix->mDeviceAddress.string(); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1816 | 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] | 1817 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1818 | } else { | 
|  | 1819 | newDeviceType = policyMix->mDeviceType; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1820 | } | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 1821 | sp device = mAvailableOutputDevices.getDevice(newDeviceType, String8(address), | 
|  | 1822 | AUDIO_FORMAT_DEFAULT); | 
|  | 1823 | ALOG_ASSERT(device, "%s: no device found t=%u, a=%s", __func__, newDeviceType, address); | 
|  | 1824 | devices.add(device); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1825 | } | 
|  | 1826 |  | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1827 | // requiresMuteCheck is false when we can bypass mute strategy. | 
|  | 1828 | // It covers a common case when there is no materially active audio | 
|  | 1829 | // and muting would result in unnecessary delay and dropped audio. | 
|  | 1830 | const uint32_t outputLatencyMs = outputDesc->latency(); | 
|  | 1831 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2);  // account for drain | 
|  | 1832 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1833 | // increment usage count for this stream on the requested output: | 
|  | 1834 | // NOTE that the usage count is the same for duplicated output and hardware output which is | 
|  | 1835 | // 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] | 1836 | outputDesc->setClientActive(client, true); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1837 |  | 
|  | 1838 | if (client->hasPreferredDevice(true)) { | 
| François Gaffie | f96e543 | 2019-04-09 17:13:56 +0200 | [diff] [blame] | 1839 | if (outputDesc->clientsList(true /*activeOnly*/).size() == 1 && | 
|  | 1840 | client->isPreferredDeviceForExclusiveUse()) { | 
|  | 1841 | // Preferred device may be exclusive, use only if no other active clients on this output | 
|  | 1842 | devices = DeviceVector( | 
|  | 1843 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId())); | 
|  | 1844 | } else { | 
|  | 1845 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); | 
|  | 1846 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1847 | if (devices != outputDesc->devices()) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1848 | checkStrategyRoute(clientStrategy, outputDesc->mIoHandle); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1849 | } | 
|  | 1850 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1851 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1852 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_MEDIA))) { | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1853 | selectOutputForMusicEffects(); | 
|  | 1854 | } | 
|  | 1855 |  | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1856 | if (outputDesc->getActivityCount(clientVolSrc) == 1 || !devices.isEmpty()) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1857 | // starting an output being rerouted? | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1858 | if (devices.isEmpty()) { | 
|  | 1859 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1860 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1861 | bool shouldWait = | 
|  | 1862 | (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM)) || | 
|  | 1863 | followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_NOTIFICATION)) || | 
|  | 1864 | (beaconMuteLatency > 0)); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1865 | uint32_t waitMs = beaconMuteLatency; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1866 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1867 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1868 | if (desc != outputDesc) { | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1869 | // An output has a shared device if | 
|  | 1870 | // - managed by the same hw module | 
|  | 1871 | // - supports the currently selected device | 
|  | 1872 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1873 | && (!desc->filterSupportedDevices(devices).isEmpty()); | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1874 |  | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1875 | // force a device change if any other output is: | 
|  | 1876 | // - managed by the same hw module | 
| Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1877 | // - supports currently selected device | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1878 | // - has a current device selection that differs from selected device. | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1879 | // - has an active audio patch | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1880 | // 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] | 1881 | // change the device currently selected by the other output. | 
|  | 1882 | if (sharedDevice && | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1883 | desc->devices() != devices && | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1884 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1885 | force = true; | 
|  | 1886 | } | 
|  | 1887 | // 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] | 1888 | // a notification so that audio focus effect can propagate, or that a mute/unmute | 
|  | 1889 | // event occurred for beacon | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1890 | const uint32_t latencyMs = desc->latency(); | 
|  | 1891 | const bool isActive = desc->isActive(latencyMs * 2);  // account for drain | 
|  | 1892 |  | 
|  | 1893 | if (shouldWait && isActive && (waitMs < latencyMs)) { | 
|  | 1894 | waitMs = latencyMs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1895 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1896 |  | 
|  | 1897 | // Require mute check if another output is on a shared device | 
|  | 1898 | // and currently active to have proper drain and avoid pops. | 
|  | 1899 | // Note restoring AudioTracks onto this output needs to invoke | 
|  | 1900 | // a volume ramp if there is no mute. | 
|  | 1901 | requiresMuteCheck |= sharedDevice && isActive; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1902 | } | 
|  | 1903 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1904 |  | 
|  | 1905 | const uint32_t muteWaitMs = | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1906 | setOutputDevices(outputDesc, devices, force, 0, NULL, requiresMuteCheck); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1907 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1908 | // apply volume rules for current stream and device if necessary | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 1909 | auto &curves = getVolumeCurves(client->attributes()); | 
|  | 1910 | checkAndSetVolume(curves, client->volumeSource(), | 
|  | 1911 | curves.getVolumeIndex(outputDesc->devices().types()), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1912 | outputDesc, | 
| Francois Gaffie | d11442b | 2020-04-27 11:51:09 +0200 | [diff] [blame] | 1913 | outputDesc->devices().types(), 0 /*delay*/, | 
|  | 1914 | outputDesc->useHwGain() /*force*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1915 |  | 
|  | 1916 | // update the outputs if starting an output with a stream that can affect notification | 
|  | 1917 | // routing | 
|  | 1918 | handleNotificationRoutingForStream(stream); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1919 |  | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1920 | // force reevaluating accessibility routing when ringtone or alarm starts | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1921 | if (followsSameRouting(clientAttr, attributes_initializer(AUDIO_USAGE_ALARM))) { | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1922 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
|  | 1923 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1924 |  | 
|  | 1925 | if (waitMs > muteWaitMs) { | 
|  | 1926 | *delayMs = waitMs - muteWaitMs; | 
|  | 1927 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1928 |  | 
|  | 1929 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. | 
|  | 1930 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes | 
|  | 1931 | // via AudioCommandThread to AudioFlinger.  Hence it is possible that the volume | 
|  | 1932 | // change occurs after the MixerThread starts and causes a stream volume | 
|  | 1933 | // glitch. | 
|  | 1934 | // | 
|  | 1935 | // We do not introduce additional delay here. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1936 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1937 |  | 
| Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1938 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1939 | mEngine->getForceUse( | 
|  | 1940 | AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 1941 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), true, outputDesc); | 
| Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1942 | } | 
|  | 1943 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1944 | // Automatically enable the remote submix input when output is started on a re routing mix | 
|  | 1945 | // of type MIX_TYPE_RECORDERS | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1946 | if (isSingleDeviceType(devices.types(), &audio_is_remote_submix_device) && | 
|  | 1947 | policyMix != NULL && policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1948 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1949 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 1950 | address, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1951 | "remote-submix", | 
|  | 1952 | AUDIO_FORMAT_DEFAULT); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1953 | } | 
|  | 1954 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1955 | return NO_ERROR; | 
|  | 1956 | } | 
|  | 1957 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1958 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1959 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1960 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 1961 |  | 
|  | 1962 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); | 
|  | 1963 | if (outputDesc == 0) { | 
|  | 1964 | ALOGW("stopOutput() no output for client %d", portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1965 | return BAD_VALUE; | 
|  | 1966 | } | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1967 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1968 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1969 | ALOGV("stopOutput() output %d, stream %d, session %d", | 
|  | 1970 | outputDesc->mIoHandle, client->stream(), client->session()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1971 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1972 | status_t status = stopSource(outputDesc, client); | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1973 |  | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1974 | if (status == NO_ERROR ) { | 
|  | 1975 | outputDesc->stop(); | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1976 | } | 
|  | 1977 | return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1978 | } | 
|  | 1979 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1980 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, | 
|  | 1981 | const sp<TrackClientDescriptor>& client) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1982 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1983 | // always handle stream stop, check which stream type is stopping | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1984 | audio_stream_type_t stream = client->stream(); | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1985 | auto clientVolSrc = client->volumeSource(); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1986 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1987 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); | 
|  | 1988 |  | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 1989 | if (outputDesc->getActivityCount(clientVolSrc) > 0) { | 
|  | 1990 | if (outputDesc->getActivityCount(clientVolSrc) == 1) { | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1991 | // Automatically disable the remote submix input when output is stopped on a | 
|  | 1992 | // re routing mix of type MIX_TYPE_RECORDERS | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 1993 | sp<AudioPolicyMix> policyMix = outputDesc->mPolicyMix.promote(); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 1994 | if (isSingleDeviceType( | 
|  | 1995 | outputDesc->devices().types(), &audio_is_remote_submix_device) && | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 1996 | policyMix != nullptr && | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 1997 | policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1998 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1999 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2000 | policyMix->mDeviceAddress, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2001 | "remote-submix", AUDIO_FORMAT_DEFAULT); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2002 | } | 
|  | 2003 | } | 
|  | 2004 | bool forceDeviceUpdate = false; | 
|  | 2005 | if (client->hasPreferredDevice(true)) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2006 | checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2007 | forceDeviceUpdate = true; | 
|  | 2008 | } | 
|  | 2009 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2010 | // decrement usage count of this stream on the output | 
| Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 2011 | outputDesc->setClientActive(client, false); | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 2012 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2013 | // store time at which the stream was stopped - see isStreamActive() | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 2014 | if (outputDesc->getActivityCount(clientVolSrc) == 0 || forceDeviceUpdate) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2015 | outputDesc->setStopTime(client, systemTime()); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2016 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2017 | // delay the device switch by twice the latency because stopOutput() is executed when | 
|  | 2018 | // the track stop() command is received and at that time the audio track buffer can | 
|  | 2019 | // still contain data that needs to be drained. The latency only covers the audio HAL | 
|  | 2020 | // and kernel buffers. Also the latency does not always include additional delay in the | 
|  | 2021 | // audio path (audio DSP, CODEC ...) | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2022 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2023 |  | 
|  | 2024 | // force restoring the device selection on other active outputs if it differs from the | 
|  | 2025 | // one being selected for this output | 
| Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2026 | uint32_t delayMs = outputDesc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2027 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2028 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2029 | if (desc != outputDesc && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2030 | desc->isActive() && | 
|  | 2031 | outputDesc->sharesHwModuleWith(desc) && | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2032 | (newDevices != desc->devices())) { | 
|  | 2033 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); | 
|  | 2034 | bool force = desc->devices() != newDevices2; | 
| Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 2035 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2036 | setOutputDevices(desc, newDevices2, force, delayMs); | 
|  | 2037 |  | 
| Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2038 | // re-apply device specific volume if not done by setOutputDevice() | 
|  | 2039 | if (!force) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2040 | applyStreamVolumes(desc, newDevices2.types(), delayMs); | 
| Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 2041 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2042 | } | 
|  | 2043 | } | 
|  | 2044 | // update the outputs if stopping one with a stream that can affect notification routing | 
|  | 2045 | handleNotificationRoutingForStream(stream); | 
|  | 2046 | } | 
| Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2047 |  | 
|  | 2048 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && | 
|  | 2049 | 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] | 2050 | setStrategyMute(streamToStrategy(AUDIO_STREAM_ALARM), false, outputDesc); | 
| Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 2051 | } | 
|  | 2052 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2053 | if (followsSameRouting(client->attributes(), attributes_initializer(AUDIO_USAGE_MEDIA))) { | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2054 | selectOutputForMusicEffects(); | 
|  | 2055 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2056 | return NO_ERROR; | 
|  | 2057 | } else { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2058 | ALOGW("stopOutput() refcount is already 0"); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2059 | return INVALID_OPERATION; | 
|  | 2060 | } | 
|  | 2061 | } | 
|  | 2062 |  | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2063 | bool AudioPolicyManager::releaseOutput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2064 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2065 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 2066 |  | 
|  | 2067 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); | 
|  | 2068 | if (outputDesc == 0) { | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2069 | // If an output descriptor is closed due to a device routing change, | 
|  | 2070 | // then there are race conditions with releaseOutput from tracks | 
|  | 2071 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread | 
|  | 2072 | // destroyed shortly thereafter. | 
|  | 2073 | // | 
|  | 2074 | // Here we just log a warning, instead of a fatal error. | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2075 | ALOGW("releaseOutput() no output for client %d", portId); | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2076 | return false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2077 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2078 |  | 
|  | 2079 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2080 |  | 
| Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2081 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); | 
|  | 2082 | if (outputDesc->isClientActive(client)) { | 
|  | 2083 | ALOGW("releaseOutput() inactivates portId %d in good faith", portId); | 
|  | 2084 | stopOutput(portId); | 
|  | 2085 | } | 
|  | 2086 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2087 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
|  | 2088 | if (outputDesc->mDirectOpenCount <= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2089 | ALOGW("releaseOutput() invalid open count %d for output %d", | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2090 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2091 | return false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2092 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2093 | if (--outputDesc->mDirectOpenCount == 0) { | 
|  | 2094 | closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2095 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2096 | } | 
|  | 2097 | } | 
| Jaideep Sharma | 7bf382e | 2020-11-26 17:07:29 +0530 | [diff] [blame] | 2098 |  | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2099 | outputDesc->removeClient(portId); | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 2100 | if (outputDesc->mPendingReopenToQueryProfiles && outputDesc->getClientCount() == 0) { | 
|  | 2101 | // The output is pending reopened to query dynamic profiles and | 
|  | 2102 | // there is no active clients | 
|  | 2103 | closeOutput(outputDesc->mIoHandle); | 
|  | 2104 | sp<SwAudioOutputDescriptor> newOutputDesc = openOutputWithProfileAndDevice( | 
|  | 2105 | outputDesc->mProfile, mEngine->getActiveMediaDevices(mAvailableOutputDevices)); | 
|  | 2106 | if (newOutputDesc == nullptr) { | 
|  | 2107 | ALOGE("%s failed to open output", __func__); | 
|  | 2108 | } | 
|  | 2109 | return true; | 
|  | 2110 | } | 
|  | 2111 | return false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2112 | } | 
|  | 2113 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2114 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, | 
|  | 2115 | audio_io_handle_t *input, | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2116 | audio_unique_id_t riid, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 2117 | audio_session_t session, | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2118 | const AttributionSourceState& attributionSource, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2119 | const audio_config_base_t *config, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2120 | audio_input_flags_t flags, | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2121 | audio_port_handle_t *selectedDeviceId, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2122 | input_type_t *inputType, | 
|  | 2123 | audio_port_handle_t *portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2124 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2125 | ALOGV("%s() source %d, sampling rate %d, format %#x, channel mask %#x, session %d, " | 
|  | 2126 | "flags %#x attributes=%s", __func__, attr->source, config->sample_rate, | 
|  | 2127 | config->format, config->channel_mask, session, flags, toString(*attr).c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2128 |  | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2129 | status_t status = NO_ERROR; | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 2130 | audio_source_t halInputSource; | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2131 | audio_attributes_t attributes = *attr; | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2132 | sp<AudioPolicyMix> policyMix; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2133 | sp<DeviceDescriptor> device; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2134 | sp<AudioInputDescriptor> inputDesc; | 
|  | 2135 | sp<RecordClientDescriptor> clientDesc; | 
|  | 2136 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; | 
| Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 2137 | uid_t uid = VALUE_OR_RETURN_STATUS(aidl2legacy_int32_t_uid_t(attributionSource.uid)); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2138 | bool isSoundTrigger; | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2139 |  | 
|  | 2140 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE | 
|  | 2141 | if (*portId != AUDIO_PORT_HANDLE_NONE) { | 
|  | 2142 | return INVALID_OPERATION; | 
|  | 2143 | } | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2144 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2145 | if (attr->source == AUDIO_SOURCE_DEFAULT) { | 
|  | 2146 | attributes.source = AUDIO_SOURCE_MIC; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2147 | } | 
|  | 2148 |  | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2149 | // Explicit routing? | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2150 | sp<DeviceDescriptor> explicitRoutingDevice = | 
|  | 2151 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2152 |  | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2153 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if | 
|  | 2154 | // possible | 
|  | 2155 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && | 
|  | 2156 | *input != AUDIO_IO_HANDLE_NONE) { | 
|  | 2157 | ssize_t index = mInputs.indexOfKey(*input); | 
|  | 2158 | if (index < 0) { | 
|  | 2159 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); | 
|  | 2160 | status = BAD_VALUE; | 
|  | 2161 | goto error; | 
|  | 2162 | } | 
|  | 2163 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2164 | RecordClientVector clients = inputDesc->getClientsForSession(session); | 
|  | 2165 | if (clients.size() == 0) { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2166 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); | 
|  | 2167 | status = BAD_VALUE; | 
|  | 2168 | goto error; | 
|  | 2169 | } | 
|  | 2170 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. | 
|  | 2171 | // 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] | 2172 | // corresponds to a new client and is only permitted from the same UID. | 
|  | 2173 | // 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] | 2174 | if (clients.size() > 1) { | 
|  | 2175 | for (const auto& client : clients) { | 
|  | 2176 | // The client map is ordered by key values (portId) and portIds are allocated | 
|  | 2177 | // incrementaly. So the first client in this list is the one opened by audio flinger | 
|  | 2178 | // when the mmap stream is created and should be ignored as it does not correspond | 
|  | 2179 | // to an actual client | 
|  | 2180 | if (client == *clients.cbegin()) { | 
|  | 2181 | continue; | 
|  | 2182 | } | 
|  | 2183 | if (uid != client->uid() && !client->isSilenced()) { | 
|  | 2184 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", | 
|  | 2185 | uid, client->portId(), client->uid()); | 
|  | 2186 | status = INVALID_OPERATION; | 
|  | 2187 | goto error; | 
|  | 2188 | } | 
| Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 2189 | } | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2190 | } | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2191 | *inputType = API_INPUT_LEGACY; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2192 | device = inputDesc->getDevice(); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2193 |  | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 2194 | ALOGV("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2195 | goto exit; | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2196 | } | 
|  | 2197 |  | 
|  | 2198 | *input = AUDIO_IO_HANDLE_NONE; | 
|  | 2199 | *inputType = API_INPUT_INVALID; | 
|  | 2200 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2201 | halInputSource = attributes.source; | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2202 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2203 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && | 
|  | 2204 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { | 
|  | 2205 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2206 | if (status != NO_ERROR) { | 
| jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2207 | ALOGW("%s could not find input mix for attr %s", | 
|  | 2208 | __func__, toString(attributes).c_str()); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2209 | goto error; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2210 | } | 
| jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 2211 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2212 | String8(attr->tags + strlen("addr=")), | 
|  | 2213 | AUDIO_FORMAT_DEFAULT); | 
|  | 2214 | if (device == nullptr) { | 
| Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 2215 | 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] | 2216 | __func__, attributes.source, attributes.tags); | 
|  | 2217 | status = BAD_VALUE; | 
|  | 2218 | goto error; | 
|  | 2219 | } | 
|  | 2220 |  | 
| Kevin Rocard | 25f9b05 | 2019-02-27 15:08:54 -0800 | [diff] [blame] | 2221 | if (is_mix_loopback_render(policyMix->mRouteFlags)) { | 
|  | 2222 | *inputType = API_INPUT_MIX_PUBLIC_CAPTURE_PLAYBACK; | 
|  | 2223 | } else { | 
|  | 2224 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
|  | 2225 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2226 | } else { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2227 | if (explicitRoutingDevice != nullptr) { | 
|  | 2228 | device = explicitRoutingDevice; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2229 | } else { | 
| Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 2230 | // Prevent from storing invalid requested device id in clients | 
|  | 2231 | requestedDeviceId = AUDIO_PORT_HANDLE_NONE; | 
| yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 2232 | device = mEngine->getInputDeviceForAttributes(attributes, uid, &policyMix); | 
| yuanjiahsu | 4069d5d | 2021-04-19 07:54:27 +0800 | [diff] [blame] | 2233 | ALOGV_IF(device != nullptr, "%s found device type is 0x%X", | 
|  | 2234 | __FUNCTION__, device->type()); | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2235 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2236 | if (device == nullptr) { | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2237 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2238 | status = BAD_VALUE; | 
|  | 2239 | goto error; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2240 | } | 
| Alden DSouza | b7d2078 | 2021-02-08 08:51:42 -0800 | [diff] [blame] | 2241 | if (device->type() == AUDIO_DEVICE_IN_ECHO_REFERENCE) { | 
|  | 2242 | *inputType = API_INPUT_MIX_CAPTURE; | 
|  | 2243 | } else if (policyMix) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2244 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); | 
|  | 2245 | // there is an external policy, but this input is attached to a mix of recorders, | 
|  | 2246 | // meaning it receives audio injected into the framework, so the recorder doesn't | 
|  | 2247 | // know about it and is therefore considered "legacy" | 
|  | 2248 | *inputType = API_INPUT_LEGACY; | 
|  | 2249 | } else if (audio_is_remote_submix_device(device->type())) { | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2250 | *inputType = API_INPUT_MIX_CAPTURE; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2251 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 2252 | *inputType = API_INPUT_TELEPHONY_RX; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 2253 | } else { | 
|  | 2254 | *inputType = API_INPUT_LEGACY; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2255 | } | 
| Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 2256 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2257 | } | 
|  | 2258 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2259 | *input = getInputForDevice(device, session, attributes, config, flags, policyMix); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2260 | if (*input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2261 | status = INVALID_OPERATION; | 
|  | 2262 | goto error; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2263 | } | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 2264 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2265 | exit: | 
|  | 2266 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2267 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? | 
|  | 2268 | device->getId() : AUDIO_PORT_HANDLE_NONE; | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2269 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2270 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && | 
| Carter Hsu | d0cce2e | 2019-05-03 17:36:28 +0800 | [diff] [blame] | 2271 | mSoundTriggerSessions.indexOfKey(session) >= 0; | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 2272 | *portId = PolicyAudioPort::getNextUniqueId(); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2273 |  | 
| Mikhail Naganov | 2996f67 | 2019-04-18 12:29:59 -0700 | [diff] [blame] | 2274 | clientDesc = new RecordClientDescriptor(*portId, riid, uid, session, attributes, *config, | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 2275 | requestedDeviceId, attributes.source, flags, | 
|  | 2276 | isSoundTrigger); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2277 | inputDesc = mInputs.valueFor(*input); | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2278 | inputDesc->addClient(clientDesc); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2279 |  | 
|  | 2280 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", | 
|  | 2281 | *input, *inputType, *selectedDeviceId, *portId); | 
| Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 2282 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2283 | return NO_ERROR; | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2284 |  | 
|  | 2285 | error: | 
| Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 2286 | return status; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2287 | } | 
|  | 2288 |  | 
|  | 2289 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2290 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2291 | audio_session_t session, | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2292 | const audio_attributes_t &attributes, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2293 | const audio_config_base_t *config, | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2294 | audio_input_flags_t flags, | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2295 | const sp<AudioPolicyMix> &policyMix) | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2296 | { | 
|  | 2297 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2298 | audio_source_t halInputSource = attributes.source; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2299 | bool isSoundTrigger = false; | 
|  | 2300 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2301 | if (attributes.source == AUDIO_SOURCE_HOTWORD) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2302 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
|  | 2303 | if (index >= 0) { | 
|  | 2304 | input = mSoundTriggerSessions.valueFor(session); | 
|  | 2305 | isSoundTrigger = true; | 
|  | 2306 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); | 
|  | 2307 | ALOGV("SoundTrigger capture on session %d input %d", session, input); | 
|  | 2308 | } else { | 
|  | 2309 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2310 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2311 | } else if (attributes.source == AUDIO_SOURCE_VOICE_COMMUNICATION && | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2312 | audio_is_linear_pcm(config->format)) { | 
| Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 2313 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2314 | } | 
|  | 2315 |  | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2316 | // find a compatible input profile (not necessarily identical in parameters) | 
|  | 2317 | sp<IOProfile> profile; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2318 | // sampling rate and flags may be updated by getInputProfile | 
|  | 2319 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? | 
|  | 2320 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 2321 | audio_format_t profileFormat; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2322 | audio_channel_mask_t profileChannelMask = config->channel_mask; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2323 | audio_input_flags_t profileFlags = flags; | 
|  | 2324 | for (;;) { | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 2325 | 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] | 2326 | profile = getInputProfile(device, profileSamplingRate, profileFormat, profileChannelMask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2327 | profileFlags); | 
|  | 2328 | if (profile != 0) { | 
|  | 2329 | break; // success | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2330 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { | 
|  | 2331 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2332 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { | 
|  | 2333 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry | 
|  | 2334 | } else { // fail | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2335 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " | 
|  | 2336 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), | 
|  | 2337 | config->sample_rate, config->format, config->channel_mask, flags); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2338 | return input; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2339 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2340 | } | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2341 | // Pick input sampling rate if not specified by client | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2342 | uint32_t samplingRate = config->sample_rate; | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2343 | if (samplingRate == 0) { | 
|  | 2344 | samplingRate = profileSamplingRate; | 
|  | 2345 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2346 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2347 | if (profile->getModuleHandle() == 0) { | 
|  | 2348 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2349 | return input; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2350 | } | 
|  | 2351 |  | 
| Eric Laurent | ec376dc | 2021-04-08 20:41:22 +0200 | [diff] [blame] | 2352 | // Reuse an already opened input if a client with the same session ID already exists | 
|  | 2353 | // on that input | 
|  | 2354 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 2355 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); | 
|  | 2356 | if (desc->mProfile != profile) { | 
|  | 2357 | continue; | 
|  | 2358 | } | 
|  | 2359 | RecordClientVector clients = desc->clientsList(); | 
|  | 2360 | for (const auto &client : clients) { | 
|  | 2361 | if (session == client->session()) { | 
|  | 2362 | return desc->mIoHandle; | 
|  | 2363 | } | 
|  | 2364 | } | 
|  | 2365 | } | 
|  | 2366 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2367 | if (!profile->canOpenNewIo()) { | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2368 | for (size_t i = 0; i < mInputs.size(); ) { | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 2369 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2370 | if (desc->mProfile != profile) { | 
| Carter Hsu | 9a645a9 | 2019-05-15 12:15:32 +0800 | [diff] [blame] | 2371 | i++; | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2372 | continue; | 
|  | 2373 | } | 
|  | 2374 | // if sound trigger, reuse input if used by other sound trigger on same session | 
|  | 2375 | // else | 
|  | 2376 | //    reuse input if active client app is not in IDLE state | 
|  | 2377 | // | 
|  | 2378 | RecordClientVector clients = desc->clientsList(); | 
|  | 2379 | bool doClose = false; | 
|  | 2380 | for (const auto& client : clients) { | 
|  | 2381 | if (isSoundTrigger != client->isSoundTrigger()) { | 
|  | 2382 | continue; | 
|  | 2383 | } | 
|  | 2384 | if (client->isSoundTrigger()) { | 
|  | 2385 | if (session == client->session()) { | 
|  | 2386 | return desc->mIoHandle; | 
|  | 2387 | } | 
|  | 2388 | continue; | 
|  | 2389 | } | 
|  | 2390 | if (client->active() && client->appState() != APP_STATE_IDLE) { | 
|  | 2391 | return desc->mIoHandle; | 
|  | 2392 | } | 
|  | 2393 | doClose = true; | 
|  | 2394 | } | 
|  | 2395 | if (doClose) { | 
|  | 2396 | closeInput(desc->mIoHandle); | 
|  | 2397 | } else { | 
|  | 2398 | i++; | 
|  | 2399 | } | 
|  | 2400 | } | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2401 | } | 
|  | 2402 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2403 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2404 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2405 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; | 
|  | 2406 | lConfig.sample_rate = profileSamplingRate; | 
|  | 2407 | lConfig.channel_mask = profileChannelMask; | 
|  | 2408 | lConfig.format = profileFormat; | 
| Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2409 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2410 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2411 |  | 
|  | 2412 | // only accept input with the exact requested set of parameters | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2413 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2414 | (profileSamplingRate != lConfig.sample_rate) || | 
|  | 2415 | !audio_formats_match(profileFormat, lConfig.format) || | 
|  | 2416 | (profileChannelMask != lConfig.channel_mask)) { | 
|  | 2417 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" | 
| Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2418 | ", format %#x, channel mask %#x", | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2419 | profileSamplingRate, profileFormat, profileChannelMask); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2420 | if (input != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2421 | inputDesc->close(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2422 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2423 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2424 | } | 
|  | 2425 |  | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2426 | inputDesc->mPolicyMix = policyMix; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2427 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2428 | addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2429 | mpClientInterface->onAudioPortListUpdate(); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2430 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2431 | return input; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2432 | } | 
|  | 2433 |  | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2434 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2435 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2436 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 2437 |  | 
|  | 2438 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); | 
|  | 2439 | if (inputDesc == 0) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2440 | ALOGW("%s no input for client %d", __FUNCTION__, portId); | 
| Eric Laurent | d52a28c | 2020-08-21 17:10:39 -0700 | [diff] [blame] | 2441 | return DEAD_OBJECT; | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2442 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2443 | audio_io_handle_t input = inputDesc->mIoHandle; | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2444 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2445 | if (client->active()) { | 
|  | 2446 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); | 
|  | 2447 | return INVALID_OPERATION; | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2448 | } | 
|  | 2449 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2450 | audio_session_t session = client->session(); | 
|  | 2451 |  | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2452 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2453 |  | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2454 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2455 |  | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2456 | status_t status = inputDesc->start(); | 
|  | 2457 | if (status != NO_ERROR) { | 
|  | 2458 | return status; | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2459 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2460 |  | 
| Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2461 | // increment activity count before calling getNewInputDevice() below as only active sessions | 
| Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2462 | // are considered for device selection | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2463 | inputDesc->setClientActive(client, true); | 
| Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2464 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2465 | // indicate active capture to sound trigger service if starting capture from a mic on | 
|  | 2466 | // primary HW module | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2467 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); | 
| Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2468 | if (device != nullptr) { | 
|  | 2469 | status = setInputDevice(input, device, true /* force */); | 
|  | 2470 | } else { | 
|  | 2471 | ALOGW("%s no new input device can be found for descriptor %d", | 
|  | 2472 | __FUNCTION__, inputDesc->getId()); | 
|  | 2473 | status = BAD_VALUE; | 
|  | 2474 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2475 |  | 
| Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2476 | if (status == NO_ERROR && inputDesc->activeCount() == 1) { | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2477 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2478 | // 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] | 2479 | if ((policyMix != nullptr) | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2480 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
|  | 2481 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2482 | MIX_STATE_MIXING); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2483 | } | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2484 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2485 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); | 
|  | 2486 | if (primaryInputDevices.contains(device) && | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2487 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { | 
| Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2488 | mpClientInterface->setSoundTriggerCaptureState(true); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2489 | } | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2490 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2491 | // automatically enable the remote submix output when input is started if not | 
|  | 2492 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 2493 | // 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] | 2494 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2495 | String8 address = String8(""); | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2496 | if (policyMix == nullptr) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2497 | address = String8("0"); | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2498 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2499 | address = policyMix->mDeviceAddress; | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2500 | } | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2501 | if (address != "") { | 
|  | 2502 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2503 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2504 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2505 | } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2506 | } | 
| Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2507 | } else if (status != NO_ERROR) { | 
|  | 2508 | // Restore client activity state. | 
|  | 2509 | inputDesc->setClientActive(client, false); | 
|  | 2510 | inputDesc->stop(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2511 | } | 
|  | 2512 |  | 
| Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2513 | ALOGV("%s input %d source = %d status = %d exit", | 
|  | 2514 | __FUNCTION__, input, client->source(), status); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2515 |  | 
| Mikhail Naganov | 480ffee | 2019-07-01 15:07:19 -0700 | [diff] [blame] | 2516 | return status; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2517 | } | 
|  | 2518 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2519 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2520 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2521 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 2522 |  | 
|  | 2523 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); | 
|  | 2524 | if (inputDesc == 0) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2525 | ALOGW("%s no input for client %d", __FUNCTION__, portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2526 | return BAD_VALUE; | 
|  | 2527 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2528 | audio_io_handle_t input = inputDesc->mIoHandle; | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2529 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2530 | if (!client->active()) { | 
|  | 2531 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2532 | return INVALID_OPERATION; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2533 | } | 
| Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2534 | auto old_source = inputDesc->source(); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2535 | inputDesc->setClientActive(client, false); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2536 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2537 | inputDesc->stop(); | 
|  | 2538 | if (inputDesc->isActive()) { | 
| Carter Hsu | e6139d5 | 2021-07-08 10:30:20 +0800 | [diff] [blame] | 2539 | auto current_source = inputDesc->source(); | 
|  | 2540 | setInputDevice(input, getNewInputDevice(inputDesc), | 
|  | 2541 | old_source != current_source /* force */); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2542 | } else { | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2543 | sp<AudioPolicyMix> policyMix = inputDesc->mPolicyMix.promote(); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2544 | // 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] | 2545 | if ((policyMix != nullptr) | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2546 | && ((policyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
|  | 2547 | mpClientInterface->onDynamicPolicyMixStateUpdate(policyMix->mDeviceAddress, | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2548 | MIX_STATE_IDLE); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2549 | } | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2550 |  | 
|  | 2551 | // automatically disable the remote submix output when input is stopped if not | 
|  | 2552 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2553 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2554 | String8 address = String8(""); | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 2555 | if (policyMix == nullptr) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2556 | address = String8("0"); | 
| Mikhail Naganov | bfac583 | 2019-03-05 16:55:28 -0800 | [diff] [blame] | 2557 | } else if (policyMix->mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2558 | address = policyMix->mDeviceAddress; | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2559 | } | 
|  | 2560 | if (address != "") { | 
|  | 2561 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2562 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2563 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2564 | } | 
|  | 2565 | } | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2566 | resetInputDevice(input); | 
|  | 2567 |  | 
|  | 2568 | // indicate inactive capture to sound trigger service if stopping capture from a mic on | 
|  | 2569 | // primary HW module | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2570 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); | 
|  | 2571 | if (primaryInputDevices.contains(inputDesc->getDevice()) && | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2572 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { | 
| Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 2573 | mpClientInterface->setSoundTriggerCaptureState(false); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2574 | } | 
|  | 2575 | inputDesc->clearPreemptedSessions(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2576 | } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2577 | return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2578 | } | 
|  | 2579 |  | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2580 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2581 | { | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2582 | ALOGV("%s portId %d", __FUNCTION__, portId); | 
|  | 2583 |  | 
|  | 2584 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); | 
|  | 2585 | if (inputDesc == 0) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2586 | ALOGW("%s no input for client %d", __FUNCTION__, portId); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2587 | return; | 
|  | 2588 | } | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2589 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2590 | audio_io_handle_t input = inputDesc->mIoHandle; | 
|  | 2591 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2592 | ALOGV("%s %d", __FUNCTION__, input); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2593 |  | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2594 | inputDesc->removeClient(portId); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2595 |  | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2596 | if (inputDesc->getClientCount() > 0) { | 
|  | 2597 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2598 | return; | 
|  | 2599 | } | 
|  | 2600 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2601 | closeInput(input); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2602 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2603 | ALOGV("%s exit", __FUNCTION__); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2604 | } | 
|  | 2605 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2606 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2607 | { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2608 | RecordClientVector clients = input->clientsList(true); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2609 |  | 
|  | 2610 | for (const auto& client : clients) { | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2611 | closeClient(client->portId()); | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2612 | } | 
|  | 2613 | } | 
|  | 2614 |  | 
| Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2615 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) | 
|  | 2616 | { | 
|  | 2617 | stopInput(portId); | 
|  | 2618 | releaseInput(portId); | 
|  | 2619 | } | 
| Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2620 |  | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2621 | void AudioPolicyManager::checkCloseInputs() { | 
|  | 2622 | // After connecting or disconnecting an input device, close input if: | 
|  | 2623 | // - it has no client (was just opened to check profile)  OR | 
|  | 2624 | // - none of its supported devices are connected anymore OR | 
|  | 2625 | // - one of its clients cannot be routed to one of its supported | 
|  | 2626 | // devices anymore. Otherwise update device selection | 
|  | 2627 | std::vector<audio_io_handle_t> inputsToClose; | 
|  | 2628 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 2629 | const sp<AudioInputDescriptor> input = mInputs.valueAt(i); | 
|  | 2630 | if (input->clientsList().size() == 0 | 
| Eric Laurent | 85732f4 | 2020-03-19 11:31:10 -0700 | [diff] [blame] | 2631 | || !mAvailableInputDevices.containsAtLeastOne(input->supportedDevices())) { | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2632 | inputsToClose.push_back(mInputs.keyAt(i)); | 
|  | 2633 | } else { | 
|  | 2634 | bool close = false; | 
|  | 2635 | for (const auto& client : input->clientsList()) { | 
|  | 2636 | sp<DeviceDescriptor> device = | 
| yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 2637 | mEngine->getInputDeviceForAttributes(client->attributes(), client->uid()); | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 2638 | if (!input->supportedDevices().contains(device)) { | 
|  | 2639 | close = true; | 
|  | 2640 | break; | 
|  | 2641 | } | 
|  | 2642 | } | 
|  | 2643 | if (close) { | 
|  | 2644 | inputsToClose.push_back(mInputs.keyAt(i)); | 
|  | 2645 | } else { | 
|  | 2646 | setInputDevice(input->mIoHandle, getNewInputDevice(input)); | 
|  | 2647 | } | 
|  | 2648 | } | 
|  | 2649 | } | 
|  | 2650 |  | 
|  | 2651 | for (const audio_io_handle_t handle : inputsToClose) { | 
|  | 2652 | ALOGV("%s closing input %d", __func__, handle); | 
|  | 2653 | closeInput(handle); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2654 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2655 | } | 
|  | 2656 |  | 
| François Gaffie | 251c7f0 | 2018-11-07 10:41:08 +0100 | [diff] [blame] | 2657 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, int indexMin, int indexMax) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2658 | { | 
|  | 2659 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
| Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 2660 | if (indexMin < 0 || indexMax < 0) { | 
|  | 2661 | ALOGE("%s for stream %d: invalid min %d or max %d", __func__, stream , indexMin, indexMax); | 
|  | 2662 | return; | 
|  | 2663 | } | 
| Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2664 | getVolumeCurves(stream).initVolume(indexMin, indexMax); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2665 |  | 
|  | 2666 | // initialize other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2667 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 2668 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2669 | continue; | 
|  | 2670 | } | 
| Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 2671 | getVolumeCurves((audio_stream_type_t)curStream).initVolume(indexMin, indexMax); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2672 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2673 | } | 
|  | 2674 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2675 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2676 | int index, | 
|  | 2677 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2678 | { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2679 | auto attributes = mEngine->getAttributesForStreamType(stream); | 
| Eric Laurent | 242a9f8 | 2020-03-23 15:57:04 -0700 | [diff] [blame] | 2680 | if (attributes == AUDIO_ATTRIBUTES_INITIALIZER) { | 
|  | 2681 | ALOGW("%s: no group for stream %s, bailing out", __func__, toString(stream).c_str()); | 
|  | 2682 | return NO_ERROR; | 
|  | 2683 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2684 | ALOGV("%s: stream %s attributes=%s", __func__, | 
|  | 2685 | toString(stream).c_str(), toString(attributes).c_str()); | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2686 | return setVolumeIndexForAttributes(attributes, index, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2687 | } | 
|  | 2688 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2689 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2690 | int *index, | 
|  | 2691 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2692 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2693 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this | 
|  | 2694 | // stream by the engine. | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2695 | DeviceTypeSet deviceTypes = {device}; | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2696 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2697 | deviceTypes = mEngine->getOutputDevicesForStream( | 
|  | 2698 | stream, true /*fromCache*/).types(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2699 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2700 | return getVolumeIndex(getVolumeCurves(stream), *index, deviceTypes); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2701 | } | 
|  | 2702 |  | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2703 | status_t AudioPolicyManager::setVolumeIndexForAttributes(const audio_attributes_t &attributes, | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2704 | int index, | 
|  | 2705 | audio_devices_t device) | 
|  | 2706 | { | 
|  | 2707 | // Get Volume group matching the Audio Attributes | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2708 | auto group = mEngine->getVolumeGroupForAttributes(attributes); | 
|  | 2709 | if (group == VOLUME_GROUP_NONE) { | 
|  | 2710 | 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] | 2711 | return BAD_VALUE; | 
|  | 2712 | } | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2713 | 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] | 2714 | status_t status = NO_ERROR; | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2715 | IVolumeCurves &curves = getVolumeCurves(attributes); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2716 | VolumeSource vs = toVolumeSource(group); | 
|  | 2717 | product_strategy_t strategy = mEngine->getProductStrategyForAttributes(attributes); | 
|  | 2718 |  | 
|  | 2719 | status = setVolumeCurveIndex(index, device, curves); | 
|  | 2720 | if (status != NO_ERROR) { | 
|  | 2721 | ALOGE("%s failed to set curve index for group %d device 0x%X", __func__, group, device); | 
|  | 2722 | return status; | 
|  | 2723 | } | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2724 |  | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2725 | DeviceTypeSet curSrcDevices; | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2726 | auto curCurvAttrs = curves.getAttributes(); | 
|  | 2727 | if (!curCurvAttrs.empty() && curCurvAttrs.front() != defaultAttr) { | 
|  | 2728 | auto attr = curCurvAttrs.front(); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2729 | curSrcDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false).types(); | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2730 | } else if (!curves.getStreamTypes().empty()) { | 
|  | 2731 | auto stream = curves.getStreamTypes().front(); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2732 | curSrcDevices = mEngine->getOutputDevicesForStream(stream, false).types(); | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2733 | } else { | 
|  | 2734 | ALOGE("%s: Invalid src %d: no valid attributes nor stream",__func__, vs); | 
|  | 2735 | return BAD_VALUE; | 
|  | 2736 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2737 | audio_devices_t curSrcDevice = Volume::getDeviceForVolume(curSrcDevices); | 
|  | 2738 | resetDeviceTypes(curSrcDevices, curSrcDevice); | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 2739 |  | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2740 | // update volume on all outputs and streams matching the following: | 
|  | 2741 | // - The requested stream (or a stream matching for volume control) is active on the output | 
|  | 2742 | // - The device (or devices) selected by the engine for this stream includes | 
|  | 2743 | // the requested device | 
|  | 2744 | // - For non default requested device, currently selected device on the output is either the | 
|  | 2745 | // requested device or one of the devices selected by the engine for this stream | 
|  | 2746 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if | 
|  | 2747 | // no specific device volume value exists for currently selected device. | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2748 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2749 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2750 | DeviceTypeSet curDevices = desc->devices().types(); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2751 |  | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2752 | if (curDevices.erase(AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { | 
|  | 2753 | curDevices.insert(AUDIO_DEVICE_OUT_SPEAKER); | 
| Robert Lee | 5e66e79 | 2019-04-03 18:37:15 +0800 | [diff] [blame] | 2754 | } | 
| François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2755 | if (!(desc->isActive(vs) || isInCall())) { | 
|  | 2756 | continue; | 
|  | 2757 | } | 
|  | 2758 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME && | 
|  | 2759 | curDevices.find(device) == curDevices.end()) { | 
|  | 2760 | continue; | 
|  | 2761 | } | 
|  | 2762 | bool applyVolume = false; | 
|  | 2763 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
|  | 2764 | curSrcDevices.insert(device); | 
|  | 2765 | applyVolume = (curSrcDevices.find( | 
|  | 2766 | Volume::getDeviceForVolume(curDevices)) != curSrcDevices.end()); | 
|  | 2767 | } else { | 
|  | 2768 | applyVolume = !curves.hasVolumeIndexForDevice(curSrcDevice); | 
|  | 2769 | } | 
|  | 2770 | if (!applyVolume) { | 
|  | 2771 | continue; // next output | 
|  | 2772 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2773 | // Inter / intra volume group priority management: Loop on strategies arranged by priority | 
|  | 2774 | // If a higher priority strategy is active, and the output is routed to a device with a | 
|  | 2775 | // HW Gain management, do not change the volume | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2776 | if (desc->useHwGain()) { | 
| François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2777 | applyVolume = false; | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2778 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { | 
|  | 2779 | auto activeClients = desc->clientsList(true /*activeOnly*/, productStrategy, | 
|  | 2780 | false /*preferredDevice*/); | 
|  | 2781 | if (activeClients.empty()) { | 
|  | 2782 | continue; | 
|  | 2783 | } | 
|  | 2784 | bool isPreempted = false; | 
|  | 2785 | bool isHigherPriority = productStrategy < strategy; | 
|  | 2786 | for (const auto &client : activeClients) { | 
|  | 2787 | if (isHigherPriority && (client->volumeSource() != vs)) { | 
|  | 2788 | ALOGV("%s: Strategy=%d (\nrequester:\n" | 
|  | 2789 | " group %d, volumeGroup=%d attributes=%s)\n" | 
|  | 2790 | " higher priority source active:\n" | 
|  | 2791 | " volumeGroup=%d attributes=%s) \n" | 
|  | 2792 | " on output %zu, bailing out", __func__, productStrategy, | 
|  | 2793 | group, group, toString(attributes).c_str(), | 
|  | 2794 | client->volumeSource(), toString(client->attributes()).c_str(), i); | 
|  | 2795 | applyVolume = false; | 
|  | 2796 | isPreempted = true; | 
|  | 2797 | break; | 
|  | 2798 | } | 
|  | 2799 | // However, continue for loop to ensure no higher prio clients running on output | 
|  | 2800 | if (client->volumeSource() == vs) { | 
|  | 2801 | applyVolume = true; | 
|  | 2802 | } | 
|  | 2803 | } | 
|  | 2804 | if (isPreempted || applyVolume) { | 
|  | 2805 | break; | 
|  | 2806 | } | 
|  | 2807 | } | 
|  | 2808 | if (!applyVolume) { | 
|  | 2809 | continue; // next output | 
|  | 2810 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2811 | } | 
| François Gaffie | ed91f58 | 2020-01-31 10:35:37 +0100 | [diff] [blame] | 2812 | //FIXME: workaround for truncated touch sounds | 
|  | 2813 | // delayed volume change for system stream to be removed when the problem is | 
|  | 2814 | // handled by system UI | 
|  | 2815 | status_t volStatus = checkAndSetVolume( | 
|  | 2816 | curves, vs, index, desc, curDevices, | 
|  | 2817 | ((vs == toVolumeSource(AUDIO_STREAM_SYSTEM))? | 
|  | 2818 | TOUCH_SOUND_FIXED_DELAY_MS : 0)); | 
|  | 2819 | if (volStatus != NO_ERROR) { | 
|  | 2820 | status = volStatus; | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2821 | } | 
|  | 2822 | } | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2823 | mpClientInterface->onAudioVolumeGroupChanged(group, 0 /*flags*/); | 
|  | 2824 | return status; | 
|  | 2825 | } | 
|  | 2826 |  | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2827 | status_t AudioPolicyManager::setVolumeCurveIndex(int index, | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2828 | audio_devices_t device, | 
|  | 2829 | IVolumeCurves &volumeCurves) | 
|  | 2830 | { | 
|  | 2831 | // VOICE_CALL stream has minVolumeIndex > 0  but can be muted directly by an | 
|  | 2832 | // app that has MODIFY_PHONE_STATE permission. | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2833 | bool hasVoice = hasVoiceStream(volumeCurves.getStreamTypes()); | 
|  | 2834 | if (((index < volumeCurves.getVolumeIndexMin()) && !(hasVoice && index == 0)) || | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2835 | (index > volumeCurves.getVolumeIndexMax())) { | 
|  | 2836 | ALOGD("%s: wrong index %d min=%d max=%d", __FUNCTION__, index, | 
|  | 2837 | volumeCurves.getVolumeIndexMin(), volumeCurves.getVolumeIndexMax()); | 
|  | 2838 | return BAD_VALUE; | 
|  | 2839 | } | 
|  | 2840 | if (!audio_is_output_device(device)) { | 
|  | 2841 | return BAD_VALUE; | 
|  | 2842 | } | 
|  | 2843 |  | 
|  | 2844 | // Force max volume if stream cannot be muted | 
|  | 2845 | if (!volumeCurves.canBeMuted()) index = volumeCurves.getVolumeIndexMax(); | 
|  | 2846 |  | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 2847 | ALOGV("%s device %08x, index %d", __FUNCTION__ , device, index); | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2848 | volumeCurves.addCurrentVolumeIndex(device, index); | 
|  | 2849 | return NO_ERROR; | 
|  | 2850 | } | 
|  | 2851 |  | 
|  | 2852 | status_t AudioPolicyManager::getVolumeIndexForAttributes(const audio_attributes_t &attr, | 
|  | 2853 | int &index, | 
|  | 2854 | audio_devices_t device) | 
|  | 2855 | { | 
|  | 2856 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device selected for this | 
|  | 2857 | // stream by the engine. | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2858 | DeviceTypeSet deviceTypes = {device}; | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2859 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2860 | DeviceTypeSet deviceTypes = mEngine->getOutputDevicesForAttributes( | 
|  | 2861 | attr, nullptr, true /*fromCache*/).types(); | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2862 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2863 | return getVolumeIndex(getVolumeCurves(attr), index, deviceTypes); | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2864 | } | 
|  | 2865 |  | 
|  | 2866 | status_t AudioPolicyManager::getVolumeIndex(const IVolumeCurves &curves, | 
|  | 2867 | int &index, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2868 | const DeviceTypeSet& deviceTypes) const | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2869 | { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2870 | if (isSingleDeviceType(deviceTypes, audio_is_output_device)) { | 
| François Gaffie | cfe1732 | 2018-11-07 13:41:29 +0100 | [diff] [blame] | 2871 | return BAD_VALUE; | 
|  | 2872 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 2873 | index = curves.getVolumeIndex(deviceTypes); | 
|  | 2874 | 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] | 2875 | return NO_ERROR; | 
|  | 2876 | } | 
|  | 2877 |  | 
|  | 2878 | status_t AudioPolicyManager::getMinVolumeIndexForAttributes(const audio_attributes_t &attr, | 
|  | 2879 | int &index) | 
|  | 2880 | { | 
|  | 2881 | index = getVolumeCurves(attr).getVolumeIndexMin(); | 
|  | 2882 | return NO_ERROR; | 
|  | 2883 | } | 
|  | 2884 |  | 
|  | 2885 | status_t AudioPolicyManager::getMaxVolumeIndexForAttributes(const audio_attributes_t &attr, | 
|  | 2886 | int &index) | 
|  | 2887 | { | 
|  | 2888 | index = getVolumeCurves(attr).getVolumeIndexMax(); | 
|  | 2889 | return NO_ERROR; | 
|  | 2890 | } | 
|  | 2891 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2892 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2893 | { | 
|  | 2894 | // select one output among several suitable for global effects. | 
|  | 2895 | // The priority is as follows: | 
|  | 2896 | // 1: An offloaded output. If the effect ends up not being offloadable, | 
|  | 2897 | //    AudioFlinger will invalidate the track and the offloaded output | 
|  | 2898 | //    will be closed causing the effect to be moved to a PCM output. | 
|  | 2899 | // 2: A deep buffer output | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2900 | // 3: The primary output | 
|  | 2901 | // 4: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2902 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2903 | DeviceVector devices = mEngine->getOutputDevicesForAttributes( | 
|  | 2904 | attributes_initializer(AUDIO_USAGE_MEDIA), nullptr, false /*fromCache*/); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2905 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2906 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2907 | if (outputs.size() == 0) { | 
|  | 2908 | return AUDIO_IO_HANDLE_NONE; | 
|  | 2909 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2910 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2911 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 2912 | bool activeOnly = true; | 
|  | 2913 |  | 
|  | 2914 | while (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 2915 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; | 
|  | 2916 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; | 
|  | 2917 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; | 
|  | 2918 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2919 | for (audio_io_handle_t output : outputs) { | 
|  | 2920 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); | 
| Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 2921 | if (activeOnly && !desc->isActive(toVolumeSource(AUDIO_STREAM_MUSIC))) { | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2922 | continue; | 
|  | 2923 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2924 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", | 
|  | 2925 | activeOnly, output, desc->mFlags); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2926 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2927 | outputOffloaded = output; | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2928 | } | 
|  | 2929 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2930 | outputDeepBuffer = output; | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2931 | } | 
|  | 2932 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2933 | outputPrimary = output; | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2934 | } | 
|  | 2935 | } | 
|  | 2936 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { | 
|  | 2937 | output = outputOffloaded; | 
|  | 2938 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { | 
|  | 2939 | output = outputDeepBuffer; | 
|  | 2940 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { | 
|  | 2941 | output = outputPrimary; | 
|  | 2942 | } else { | 
|  | 2943 | output = outputs[0]; | 
|  | 2944 | } | 
|  | 2945 | activeOnly = false; | 
|  | 2946 | } | 
|  | 2947 |  | 
|  | 2948 | if (output != mMusicEffectOutput) { | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 2949 | mEffects.moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2950 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); | 
|  | 2951 | mMusicEffectOutput = output; | 
|  | 2952 | } | 
|  | 2953 |  | 
|  | 2954 | ALOGV("selectOutputForMusicEffects selected output %d", output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2955 | return output; | 
|  | 2956 | } | 
|  | 2957 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2958 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) | 
|  | 2959 | { | 
|  | 2960 | return selectOutputForMusicEffects(); | 
|  | 2961 | } | 
|  | 2962 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2963 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2964 | audio_io_handle_t io, | 
| Ytai Ben-Tsvi | 0a4904a | 2021-01-06 12:57:05 -0800 | [diff] [blame] | 2965 | product_strategy_t strategy, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2966 | int session, | 
|  | 2967 | int id) | 
|  | 2968 | { | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 2969 | if (session != AUDIO_SESSION_DEVICE) { | 
|  | 2970 | ssize_t index = mOutputs.indexOfKey(io); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2971 | if (index < 0) { | 
| Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 2972 | index = mInputs.indexOfKey(io); | 
|  | 2973 | if (index < 0) { | 
|  | 2974 | ALOGW("registerEffect() unknown io %d", io); | 
|  | 2975 | return INVALID_OPERATION; | 
|  | 2976 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2977 | } | 
|  | 2978 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 2979 | return mEffects.registerEffect(desc, io, session, id, | 
|  | 2980 | (strategy == streamToStrategy(AUDIO_STREAM_MUSIC) || | 
|  | 2981 | strategy == PRODUCT_STRATEGY_NONE)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2982 | } | 
|  | 2983 |  | 
| Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 2984 | status_t AudioPolicyManager::unregisterEffect(int id) | 
|  | 2985 | { | 
|  | 2986 | if (mEffects.getEffect(id) == nullptr) { | 
|  | 2987 | return INVALID_OPERATION; | 
|  | 2988 | } | 
| Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 2989 | if (mEffects.isEffectEnabled(id)) { | 
|  | 2990 | ALOGW("%s effect %d enabled", __FUNCTION__, id); | 
|  | 2991 | setEffectEnabled(id, false); | 
|  | 2992 | } | 
|  | 2993 | return mEffects.unregisterEffect(id); | 
|  | 2994 | } | 
|  | 2995 |  | 
|  | 2996 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) | 
|  | 2997 | { | 
|  | 2998 | sp<EffectDescriptor> effect = mEffects.getEffect(id); | 
|  | 2999 | if (effect == nullptr) { | 
|  | 3000 | return INVALID_OPERATION; | 
|  | 3001 | } | 
|  | 3002 |  | 
|  | 3003 | status_t status = mEffects.setEffectEnabled(id, enabled); | 
|  | 3004 | if (status == NO_ERROR) { | 
|  | 3005 | mInputs.trackEffectEnabled(effect, enabled); | 
|  | 3006 | } | 
|  | 3007 | return status; | 
|  | 3008 | } | 
|  | 3009 |  | 
| Eric Laurent | 6c79632 | 2019-04-09 14:13:17 -0700 | [diff] [blame] | 3010 |  | 
|  | 3011 | status_t AudioPolicyManager::moveEffectsToIo(const std::vector<int>& ids, audio_io_handle_t io) | 
|  | 3012 | { | 
|  | 3013 | mEffects.moveEffects(ids, io); | 
|  | 3014 | return NO_ERROR; | 
|  | 3015 | } | 
|  | 3016 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3017 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 3018 | { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3019 | return mOutputs.isActive(toVolumeSource(stream), inPastMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3020 | } | 
|  | 3021 |  | 
|  | 3022 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 3023 | { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 3024 | return mOutputs.isActiveRemotely(toVolumeSource(stream), inPastMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3025 | } | 
|  | 3026 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3027 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3028 | { | 
|  | 3029 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3030 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3031 | if (inputDescriptor->isSourceActive(source)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3032 | return true; | 
|  | 3033 | } | 
|  | 3034 | } | 
|  | 3035 | return false; | 
|  | 3036 | } | 
|  | 3037 |  | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3038 | // Register a list of custom mixes with their attributes and format. | 
|  | 3039 | // When a mix is registered, corresponding input and output profiles are | 
|  | 3040 | // added to the remote submix hw module. The profile contains only the | 
|  | 3041 | // parameters (sampling rate, format...) specified by the mix. | 
|  | 3042 | // The corresponding input remote submix device is also connected. | 
|  | 3043 | // | 
|  | 3044 | // When a remote submix device is connected, the address is checked to select the | 
|  | 3045 | // appropriate profile and the corresponding input or output stream is opened. | 
|  | 3046 | // | 
|  | 3047 | // When capture starts, getInputForAttr() will: | 
|  | 3048 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 3049 | //  - 2 if none found, getDeviceForInputSource() will: | 
|  | 3050 | //     - 2.1 look for a mix matching the attributes source | 
|  | 3051 | //     - 2.2 if none found, default to device selection by policy rules | 
|  | 3052 | // At this time, the corresponding output remote submix device is also connected | 
|  | 3053 | // and active playback use cases can be transferred to this mix if needed when reconnecting | 
|  | 3054 | // after AudioTracks are invalidated | 
|  | 3055 | // | 
|  | 3056 | // When playback starts, getOutputForAttr() will: | 
|  | 3057 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 3058 | //  - 2 if none found, look for a mix matching the attributes usage | 
|  | 3059 | //  - 3 if none found, default to device and output selection by policy rules. | 
|  | 3060 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3061 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3062 | { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3063 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); | 
|  | 3064 | status_t res = NO_ERROR; | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3065 | bool checkOutputs = false; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3066 | sp<HwModule> rSubmixModule; | 
|  | 3067 | // examine each mix's route type | 
|  | 3068 | for (size_t i = 0; i < mixes.size(); i++) { | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3069 | AudioMix mix = mixes[i]; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3070 | // Only capture of playback is allowed in LOOP_BACK & RENDER mode | 
|  | 3071 | if (is_mix_loopback_render(mix.mRouteFlags) && mix.mMixType != MIX_TYPE_PLAYERS) { | 
|  | 3072 | ALOGE("Unsupported Policy Mix %zu of %zu: " | 
|  | 3073 | "Only capture of playback is allowed in LOOP_BACK & RENDER mode", | 
|  | 3074 | i, mixes.size()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3075 | res = INVALID_OPERATION; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3076 | break; | 
|  | 3077 | } | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3078 | // LOOP_BACK and LOOP_BACK | RENDER have the same remote submix backend and are handled | 
|  | 3079 | // in the same way. | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3080 | 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] | 3081 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK %d", i, mixes.size(), | 
|  | 3082 | mix.mRouteFlags); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3083 | if (rSubmixModule == 0) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3084 | rSubmixModule = mHwModules.getModuleFromName( | 
|  | 3085 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); | 
|  | 3086 | if (rSubmixModule == 0) { | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3087 | ALOGE("Unable to find audio module for submix, aborting mix %zu registration", | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3088 | i); | 
|  | 3089 | res = INVALID_OPERATION; | 
|  | 3090 | break; | 
|  | 3091 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3092 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3093 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3094 | String8 address = mix.mDeviceAddress; | 
| Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3095 | audio_devices_t deviceTypeToMakeAvailable; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3096 | if (mix.mMixType == MIX_TYPE_PLAYERS) { | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3097 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
| Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3098 | deviceTypeToMakeAvailable = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 3099 | } else { | 
|  | 3100 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 3101 | deviceTypeToMakeAvailable = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3102 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3103 |  | 
| Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3104 | if (mPolicyMixes.registerMix(mix, 0 /*output desc*/) != NO_ERROR) { | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3105 | ALOGE("Error registering mix %zu for address %s", i, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3106 | res = INVALID_OPERATION; | 
|  | 3107 | break; | 
|  | 3108 | } | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3109 | audio_config_t outputConfig = mix.mFormat; | 
|  | 3110 | audio_config_t inputConfig = mix.mFormat; | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3111 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL | 
|  | 3112 | // 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] | 3113 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; | 
|  | 3114 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3115 | rSubmixModule->addOutputProfile(address.c_str(), &outputConfig, | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3116 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3117 | rSubmixModule->addInputProfile(address.c_str(), &inputConfig, | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3118 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3119 |  | 
| Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3120 | if ((res = setDeviceConnectionStateInt(deviceTypeToMakeAvailable, | 
| jiabin | c1de2df | 2019-05-07 14:26:40 -0700 | [diff] [blame] | 3121 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 3122 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT)) != NO_ERROR) { | 
|  | 3123 | ALOGE("Failed to set remote submix device available, type %u, address %s", | 
|  | 3124 | mix.mDeviceType, address.string()); | 
|  | 3125 | break; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3126 | } | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3127 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 3128 | String8 address = mix.mDeviceAddress; | 
| Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3129 | audio_devices_t type = mix.mDeviceType; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3130 | 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] | 3131 | i, mixes.size(), type, address.string()); | 
|  | 3132 |  | 
|  | 3133 | sp<DeviceDescriptor> device = mHwModules.getDeviceDescriptor( | 
|  | 3134 | mix.mDeviceType, mix.mDeviceAddress, | 
|  | 3135 | String8(), AUDIO_FORMAT_DEFAULT); | 
|  | 3136 | if (device == nullptr) { | 
|  | 3137 | res = INVALID_OPERATION; | 
|  | 3138 | break; | 
|  | 3139 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3140 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3141 | bool foundOutput = false; | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3142 | // First try to find an already opened output supporting the device | 
|  | 3143 | 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] | 3144 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3145 |  | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3146 | if (!desc->isDuplicated() && desc->supportedDevices().contains(device)) { | 
| Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3147 | if (mPolicyMixes.registerMix(mix, desc) != NO_ERROR) { | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3148 | ALOGE("Could not register mix RENDER,  dev=0x%X addr=%s", type, | 
|  | 3149 | address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3150 | res = INVALID_OPERATION; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3151 | } else { | 
|  | 3152 | foundOutput = true; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3153 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3154 | } | 
|  | 3155 | } | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3156 | // If no output found, try to find a direct output profile supporting the device | 
|  | 3157 | for (size_t i = 0; i < mHwModules.size() && !foundOutput && res == NO_ERROR; i++) { | 
|  | 3158 | sp<HwModule> module = mHwModules[i]; | 
|  | 3159 | for (size_t j = 0; | 
|  | 3160 | j < module->getOutputProfiles().size() && !foundOutput && res == NO_ERROR; | 
|  | 3161 | j++) { | 
|  | 3162 | sp<IOProfile> profile = module->getOutputProfiles()[j]; | 
|  | 3163 | if (profile->isDirectOutput() && profile->supportsDevice(device)) { | 
|  | 3164 | if (mPolicyMixes.registerMix(mix, nullptr) != NO_ERROR) { | 
|  | 3165 | ALOGE("Could not register mix RENDER,  dev=0x%X addr=%s", type, | 
|  | 3166 | address.string()); | 
|  | 3167 | res = INVALID_OPERATION; | 
|  | 3168 | } else { | 
|  | 3169 | foundOutput = true; | 
|  | 3170 | } | 
|  | 3171 | } | 
|  | 3172 | } | 
|  | 3173 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3174 | if (res != NO_ERROR) { | 
|  | 3175 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", | 
| Eric Laurent | 2c80be0 | 2019-01-23 18:06:37 -0800 | [diff] [blame] | 3176 | i, type, address.string()); | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 3177 | res = INVALID_OPERATION; | 
|  | 3178 | break; | 
|  | 3179 | } else if (!foundOutput) { | 
|  | 3180 | 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] | 3181 | i, type, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3182 | res = INVALID_OPERATION; | 
|  | 3183 | break; | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3184 | } else { | 
|  | 3185 | checkOutputs = true; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3186 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3187 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3188 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3189 | if (res != NO_ERROR) { | 
|  | 3190 | unregisterPolicyMixes(mixes); | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3191 | } else if (checkOutputs) { | 
|  | 3192 | checkForDeviceAndOutputChanges(); | 
|  | 3193 | updateCallAndOutputRouting(); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3194 | } | 
|  | 3195 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3196 | } | 
|  | 3197 |  | 
|  | 3198 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) | 
|  | 3199 | { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3200 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3201 | status_t res = NO_ERROR; | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3202 | bool checkOutputs = false; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3203 | sp<HwModule> rSubmixModule; | 
|  | 3204 | // examine each mix's route type | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3205 | for (const auto& mix : mixes) { | 
|  | 3206 | 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] | 3207 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3208 | if (rSubmixModule == 0) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3209 | rSubmixModule = mHwModules.getModuleFromName( | 
|  | 3210 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); | 
|  | 3211 | if (rSubmixModule == 0) { | 
|  | 3212 | res = INVALID_OPERATION; | 
|  | 3213 | continue; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3214 | } | 
|  | 3215 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3216 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3217 | String8 address = mix.mDeviceAddress; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3218 |  | 
| Jean-Michel Trivi | 6791727 | 2019-05-22 11:54:37 -0700 | [diff] [blame] | 3219 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3220 | res = INVALID_OPERATION; | 
|  | 3221 | continue; | 
|  | 3222 | } | 
|  | 3223 |  | 
| Kevin Rocard | 04ed046 | 2019-05-02 17:53:24 -0700 | [diff] [blame] | 3224 | for (auto device : {AUDIO_DEVICE_IN_REMOTE_SUBMIX, AUDIO_DEVICE_OUT_REMOTE_SUBMIX}) { | 
|  | 3225 | if (getDeviceConnectionState(device, address.string()) == | 
|  | 3226 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 3227 | res = setDeviceConnectionStateInt(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 3228 | address.string(), "remote-submix", | 
|  | 3229 | AUDIO_FORMAT_DEFAULT); | 
|  | 3230 | if (res != OK) { | 
|  | 3231 | ALOGE("Error making RemoteSubmix device unavailable for mix " | 
|  | 3232 | "with type %d, address %s", device, address.string()); | 
|  | 3233 | } | 
|  | 3234 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3235 | } | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3236 | rSubmixModule->removeOutputProfile(address.c_str()); | 
|  | 3237 | rSubmixModule->removeInputProfile(address.c_str()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3238 |  | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 3239 | } 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] | 3240 | if (mPolicyMixes.unregisterMix(mix) != NO_ERROR) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3241 | res = INVALID_OPERATION; | 
|  | 3242 | continue; | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3243 | } else { | 
|  | 3244 | checkOutputs = true; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3245 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3246 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3247 | } | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 3248 | if (res == NO_ERROR && checkOutputs) { | 
|  | 3249 | checkForDeviceAndOutputChanges(); | 
|  | 3250 | updateCallAndOutputRouting(); | 
|  | 3251 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 3252 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3253 | } | 
|  | 3254 |  | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3255 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const | 
|  | 3256 | { | 
|  | 3257 | size_t i = 0; | 
|  | 3258 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); | 
|  | 3259 | for (const auto& fmt : mManualSurroundFormats) { | 
|  | 3260 | if (i++ != 0) dst->append(", "); | 
|  | 3261 | std::string sfmt; | 
|  | 3262 | FormatConverter::toString(fmt, sfmt); | 
|  | 3263 | dst->append(sfmt.size() >= audioFormatPrefixLen ? | 
|  | 3264 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); | 
|  | 3265 | } | 
|  | 3266 | } | 
|  | 3267 |  | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3268 | // Returns true if all devices types match the predicate and are supported by one HW module | 
|  | 3269 | bool  AudioPolicyManager::areAllDevicesSupported( | 
| jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3270 | const AudioDeviceTypeAddrVector& devices, | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3271 | std::function<bool(audio_devices_t)> predicate, | 
|  | 3272 | const char *context) { | 
|  | 3273 | for (size_t i = 0; i < devices.size(); i++) { | 
|  | 3274 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3275 | devices[i].mType, devices[i].getAddress(), String8(), | 
| Eric Laurent | 0e26e3f | 2020-04-29 14:24:16 -0700 | [diff] [blame] | 3276 | AUDIO_FORMAT_DEFAULT, false /*allowToCreate*/, true /*matchAddress*/); | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3277 | if (devDesc == nullptr || (predicate != nullptr && !predicate(devices[i].mType))) { | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3278 | ALOGE("%s: device type %#x address %s not supported or not match predicate", | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3279 | context, devices[i].mType, devices[i].getAddress()); | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3280 | return false; | 
|  | 3281 | } | 
|  | 3282 | } | 
|  | 3283 | return true; | 
|  | 3284 | } | 
|  | 3285 |  | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3286 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, | 
| jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3287 | const AudioDeviceTypeAddrVector& devices) { | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3288 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3289 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { | 
|  | 3290 | return BAD_VALUE; | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3291 | } | 
|  | 3292 | status_t res =  mPolicyMixes.setUidDeviceAffinities(uid, devices); | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3293 | if (res != NO_ERROR) { | 
|  | 3294 | ALOGE("%s() Could not set all device affinities for uid = %d", __FUNCTION__, uid); | 
|  | 3295 | return res; | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3296 | } | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3297 |  | 
|  | 3298 | checkForDeviceAndOutputChanges(); | 
|  | 3299 | updateCallAndOutputRouting(); | 
|  | 3300 |  | 
|  | 3301 | return NO_ERROR; | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3302 | } | 
|  | 3303 |  | 
|  | 3304 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { | 
|  | 3305 | ALOGV("%s() uid=%d", __FUNCTION__, uid); | 
| Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3306 | status_t res = mPolicyMixes.removeUidDeviceAffinities(uid); | 
|  | 3307 | if (res != NO_ERROR) { | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3308 | ALOGE("%s() Could not remove all device affinities for uid = %d", | 
| Oscar Azucena | 4b2a821 | 2019-04-26 23:48:59 -0700 | [diff] [blame] | 3309 | __FUNCTION__, uid); | 
|  | 3310 | return INVALID_OPERATION; | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3311 | } | 
|  | 3312 |  | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3313 | checkForDeviceAndOutputChanges(); | 
|  | 3314 | updateCallAndOutputRouting(); | 
|  | 3315 |  | 
| Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 3316 | return res; | 
|  | 3317 | } | 
|  | 3318 |  | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3319 |  | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3320 | status_t AudioPolicyManager::setDevicesRoleForStrategy(product_strategy_t strategy, | 
|  | 3321 | device_role_t role, | 
|  | 3322 | const AudioDeviceTypeAddrVector &devices) { | 
|  | 3323 | ALOGV("%s() strategy=%d role=%d %s", __func__, strategy, role, | 
|  | 3324 | dumpAudioDeviceTypeAddrVector(devices).c_str()); | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3325 |  | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3326 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3327 | return BAD_VALUE; | 
|  | 3328 | } | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3329 | status_t status = mEngine->setDevicesRoleForStrategy(strategy, role, devices); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3330 | if (status != NO_ERROR) { | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3331 | ALOGW("Engine could not set preferred devices %s for strategy %d role %d", | 
|  | 3332 | dumpAudioDeviceTypeAddrVector(devices).c_str(), strategy, role); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3333 | return status; | 
|  | 3334 | } | 
|  | 3335 |  | 
|  | 3336 | checkForDeviceAndOutputChanges(); | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3337 |  | 
|  | 3338 | bool forceVolumeReeval = false; | 
|  | 3339 | // FIXME: workaround for truncated touch sounds | 
|  | 3340 | // to be removed when the problem is handled by system UI | 
|  | 3341 | uint32_t delayMs = 0; | 
|  | 3342 | if (strategy == mCommunnicationStrategy) { | 
|  | 3343 | forceVolumeReeval = true; | 
|  | 3344 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; | 
|  | 3345 | updateInputRouting(); | 
|  | 3346 | } | 
|  | 3347 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3348 |  | 
|  | 3349 | return NO_ERROR; | 
|  | 3350 | } | 
|  | 3351 |  | 
|  | 3352 | void AudioPolicyManager::updateCallAndOutputRouting(bool forceVolumeReeval, uint32_t delayMs) | 
|  | 3353 | { | 
|  | 3354 | uint32_t waitMs = 0; | 
| Francois Gaffie | 19fd6c5 | 2021-02-04 17:02:59 +0100 | [diff] [blame] | 3355 | if (updateCallRouting(true /*fromCache*/, delayMs, &waitMs) == NO_ERROR) { | 
| Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3356 | // Only apply special touch sound delay once | 
|  | 3357 | delayMs = 0; | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3358 | } | 
|  | 3359 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3360 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
|  | 3361 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); | 
|  | 3362 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { | 
|  | 3363 | // As done in setDeviceConnectionState, we could also fix default device issue by | 
|  | 3364 | // preventing the force re-routing in case of default dev that distinguishes on address. | 
|  | 3365 | // Let's give back to engine full device choice decision however. | 
|  | 3366 | waitMs = setOutputDevices(outputDesc, newDevices, !newDevices.isEmpty(), delayMs); | 
| Eric Laurent | b36b4ac | 2020-08-21 12:50:41 -0700 | [diff] [blame] | 3367 | // Only apply special touch sound delay once | 
|  | 3368 | delayMs = 0; | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3369 | } | 
|  | 3370 | if (forceVolumeReeval && !newDevices.isEmpty()) { | 
|  | 3371 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); | 
|  | 3372 | } | 
|  | 3373 | } | 
|  | 3374 | } | 
|  | 3375 |  | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3376 | void AudioPolicyManager::updateInputRouting() { | 
|  | 3377 | for (const auto& activeDesc : mInputs.getActiveInputs()) { | 
| Jaideep Sharma | 408349a | 2020-11-27 14:47:17 +0530 | [diff] [blame] | 3378 | // Skip for hotword recording as the input device switch | 
|  | 3379 | // is handled within sound trigger HAL | 
|  | 3380 | if (activeDesc->isSoundTrigger() && activeDesc->source() == AUDIO_SOURCE_HOTWORD) { | 
|  | 3381 | continue; | 
|  | 3382 | } | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3383 | auto newDevice = getNewInputDevice(activeDesc); | 
|  | 3384 | // Force new input selection if the new device can not be reached via current input | 
|  | 3385 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { | 
|  | 3386 | setInputDevice(activeDesc->mIoHandle, newDevice); | 
|  | 3387 | } else { | 
|  | 3388 | closeInput(activeDesc->mIoHandle); | 
|  | 3389 | } | 
|  | 3390 | } | 
|  | 3391 | } | 
|  | 3392 |  | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3393 | status_t AudioPolicyManager::removeDevicesRoleForStrategy(product_strategy_t strategy, | 
|  | 3394 | device_role_t role) | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3395 | { | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3396 | ALOGV("%s() strategy=%d role=%d", __func__, strategy, role); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3397 |  | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3398 | status_t status = mEngine->removeDevicesRoleForStrategy(strategy, role); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3399 | if (status != NO_ERROR) { | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3400 | ALOGV("Engine could not remove preferred device for strategy %d status %d", | 
|  | 3401 | strategy, status); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3402 | return status; | 
|  | 3403 | } | 
|  | 3404 |  | 
|  | 3405 | checkForDeviceAndOutputChanges(); | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3406 |  | 
|  | 3407 | bool forceVolumeReeval = false; | 
|  | 3408 | // FIXME: workaround for truncated touch sounds | 
|  | 3409 | // to be removed when the problem is handled by system UI | 
|  | 3410 | uint32_t delayMs = 0; | 
|  | 3411 | if (strategy == mCommunnicationStrategy) { | 
|  | 3412 | forceVolumeReeval = true; | 
|  | 3413 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; | 
|  | 3414 | updateInputRouting(); | 
|  | 3415 | } | 
|  | 3416 | updateCallAndOutputRouting(forceVolumeReeval, delayMs); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3417 |  | 
|  | 3418 | return NO_ERROR; | 
|  | 3419 | } | 
|  | 3420 |  | 
| jiabin | 0a48893 | 2020-08-07 17:32:40 -0700 | [diff] [blame] | 3421 | status_t AudioPolicyManager::getDevicesForRoleAndStrategy(product_strategy_t strategy, | 
|  | 3422 | device_role_t role, | 
|  | 3423 | AudioDeviceTypeAddrVector &devices) { | 
|  | 3424 | return mEngine->getDevicesForRoleAndStrategy(strategy, role, devices); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 3425 | } | 
|  | 3426 |  | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3427 | status_t AudioPolicyManager::setDevicesRoleForCapturePreset( | 
|  | 3428 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { | 
|  | 3429 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, | 
|  | 3430 | dumpAudioDeviceTypeAddrVector(devices).c_str()); | 
|  | 3431 |  | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3432 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3433 | return BAD_VALUE; | 
|  | 3434 | } | 
|  | 3435 | status_t status = mEngine->setDevicesRoleForCapturePreset(audioSource, role, devices); | 
|  | 3436 | ALOGW_IF(status != NO_ERROR, | 
|  | 3437 | "Engine could not set preferred devices %s for audio source %d role %d", | 
|  | 3438 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); | 
|  | 3439 |  | 
|  | 3440 | return status; | 
|  | 3441 | } | 
|  | 3442 |  | 
|  | 3443 | status_t AudioPolicyManager::addDevicesRoleForCapturePreset( | 
|  | 3444 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector &devices) { | 
|  | 3445 | ALOGV("%s() audioSource=%d role=%d %s", __func__, audioSource, role, | 
|  | 3446 | dumpAudioDeviceTypeAddrVector(devices).c_str()); | 
|  | 3447 |  | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3448 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3449 | return BAD_VALUE; | 
|  | 3450 | } | 
|  | 3451 | status_t status = mEngine->addDevicesRoleForCapturePreset(audioSource, role, devices); | 
|  | 3452 | ALOGW_IF(status != NO_ERROR, | 
|  | 3453 | "Engine could not add preferred devices %s for audio source %d role %d", | 
|  | 3454 | dumpAudioDeviceTypeAddrVector(devices).c_str(), audioSource, role); | 
|  | 3455 |  | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3456 | updateInputRouting(); | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3457 | return status; | 
|  | 3458 | } | 
|  | 3459 |  | 
|  | 3460 | status_t AudioPolicyManager::removeDevicesRoleForCapturePreset( | 
|  | 3461 | audio_source_t audioSource, device_role_t role, const AudioDeviceTypeAddrVector& devices) | 
|  | 3462 | { | 
|  | 3463 | ALOGV("%s() audioSource=%d role=%d devices=%s", __func__, audioSource, role, | 
|  | 3464 | dumpAudioDeviceTypeAddrVector(devices).c_str()); | 
|  | 3465 |  | 
| Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 3466 | if (!areAllDevicesSupported(devices, audio_call_is_input_device, __func__)) { | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3467 | return BAD_VALUE; | 
|  | 3468 | } | 
|  | 3469 |  | 
|  | 3470 | status_t status = mEngine->removeDevicesRoleForCapturePreset( | 
|  | 3471 | audioSource, role, devices); | 
|  | 3472 | ALOGW_IF(status != NO_ERROR, | 
|  | 3473 | "Engine could not remove devices role (%d) for capture preset %d", role, audioSource); | 
|  | 3474 |  | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3475 | updateInputRouting(); | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3476 | return status; | 
|  | 3477 | } | 
|  | 3478 |  | 
|  | 3479 | status_t AudioPolicyManager::clearDevicesRoleForCapturePreset(audio_source_t audioSource, | 
|  | 3480 | device_role_t role) { | 
|  | 3481 | ALOGV("%s() audioSource=%d role=%d", __func__, audioSource, role); | 
|  | 3482 |  | 
|  | 3483 | status_t status = mEngine->clearDevicesRoleForCapturePreset(audioSource, role); | 
|  | 3484 | ALOGW_IF(status != NO_ERROR, | 
|  | 3485 | "Engine could not clear devices role (%d) for capture preset %d", role, audioSource); | 
|  | 3486 |  | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3487 | updateInputRouting(); | 
| Jiabin Huang | 3b98d32 | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 3488 | return status; | 
|  | 3489 | } | 
|  | 3490 |  | 
|  | 3491 | status_t AudioPolicyManager::getDevicesForRoleAndCapturePreset( | 
|  | 3492 | audio_source_t audioSource, device_role_t role, AudioDeviceTypeAddrVector &devices) { | 
|  | 3493 | return mEngine->getDevicesForRoleAndCapturePreset(audioSource, role, devices); | 
|  | 3494 | } | 
|  | 3495 |  | 
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3496 | status_t AudioPolicyManager::setUserIdDeviceAffinities(int userId, | 
| jiabin | 6a02d53 | 2020-08-07 11:56:38 -0700 | [diff] [blame] | 3497 | const AudioDeviceTypeAddrVector& devices) { | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3498 | ALOGV("%s() userId=%d num devices %zu", __func__, userId, devices.size()); | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 3499 | if (!areAllDevicesSupported(devices, audio_is_output_device, __func__)) { | 
|  | 3500 | return BAD_VALUE; | 
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3501 | } | 
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3502 | status_t status =  mPolicyMixes.setUserIdDeviceAffinities(userId, devices); | 
|  | 3503 | if (status != NO_ERROR) { | 
|  | 3504 | ALOGE("%s() could not set device affinity for userId %d", | 
|  | 3505 | __FUNCTION__, userId); | 
|  | 3506 | return status; | 
|  | 3507 | } | 
|  | 3508 |  | 
|  | 3509 | // reevaluate outputs for all devices | 
|  | 3510 | checkForDeviceAndOutputChanges(); | 
|  | 3511 | updateCallAndOutputRouting(); | 
|  | 3512 |  | 
|  | 3513 | return NO_ERROR; | 
|  | 3514 | } | 
|  | 3515 |  | 
|  | 3516 | status_t AudioPolicyManager::removeUserIdDeviceAffinities(int userId) { | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 3517 | ALOGV("%s() userId=%d", __FUNCTION__, userId); | 
| Oscar Azucena | 90e7763 | 2019-11-27 17:12:28 -0800 | [diff] [blame] | 3518 | status_t status = mPolicyMixes.removeUserIdDeviceAffinities(userId); | 
|  | 3519 | if (status != NO_ERROR) { | 
|  | 3520 | ALOGE("%s() Could not remove all device affinities fo userId = %d", | 
|  | 3521 | __FUNCTION__, userId); | 
|  | 3522 | return status; | 
|  | 3523 | } | 
|  | 3524 |  | 
|  | 3525 | // reevaluate outputs for all devices | 
|  | 3526 | checkForDeviceAndOutputChanges(); | 
|  | 3527 | updateCallAndOutputRouting(); | 
|  | 3528 |  | 
|  | 3529 | return NO_ERROR; | 
|  | 3530 | } | 
|  | 3531 |  | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3532 | void AudioPolicyManager::dump(String8 *dst) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3533 | { | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3534 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); | 
|  | 3535 | dst->appendFormat(" Primary Output: %d\n", | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3536 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); | 
| Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 3537 | std::string stateLiteral; | 
|  | 3538 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3539 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); | 
| Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3540 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { | 
|  | 3541 | "communications", "media", "record", "dock", "system", | 
|  | 3542 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; | 
|  | 3543 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; | 
|  | 3544 | 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] | 3545 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); | 
|  | 3546 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); | 
|  | 3547 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && | 
|  | 3548 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { | 
|  | 3549 | dst->append(" (MANUAL: "); | 
|  | 3550 | dumpManualSurroundFormats(dst); | 
|  | 3551 | dst->append(")"); | 
|  | 3552 | } | 
|  | 3553 | dst->append("\n"); | 
| Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3554 | } | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3555 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); | 
|  | 3556 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 3557 | dst->appendFormat(" Communnication Strategy: %d\n", mCommunnicationStrategy); | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3558 | 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] | 3559 |  | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3560 | mAvailableOutputDevices.dump(dst, String8("Available output")); | 
|  | 3561 | mAvailableInputDevices.dump(dst, String8("Available input")); | 
|  | 3562 | mHwModulesAll.dump(dst); | 
|  | 3563 | mOutputs.dump(dst); | 
|  | 3564 | mInputs.dump(dst); | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3565 | mEffects.dump(dst); | 
|  | 3566 | mAudioPatches.dump(dst); | 
|  | 3567 | mPolicyMixes.dump(dst); | 
|  | 3568 | mAudioSources.dump(dst); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3569 |  | 
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3570 | dst->appendFormat(" AllowedCapturePolicies:\n"); | 
|  | 3571 | for (auto& policy : mAllowedCapturePolicies) { | 
|  | 3572 | dst->appendFormat("   - uid=%d flag_mask=%#x\n", policy.first, policy.second); | 
|  | 3573 | } | 
|  | 3574 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 3575 | dst->appendFormat("\nPolicy Engine dump:\n"); | 
|  | 3576 | mEngine->dump(dst); | 
| Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 3577 | } | 
|  | 3578 |  | 
|  | 3579 | status_t AudioPolicyManager::dump(int fd) | 
|  | 3580 | { | 
|  | 3581 | String8 result; | 
|  | 3582 | dump(&result); | 
| Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 3583 | write(fd, result.string(), result.size()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3584 | return NO_ERROR; | 
|  | 3585 | } | 
|  | 3586 |  | 
| Kevin Rocard | b99cc75 | 2019-03-21 20:52:24 -0700 | [diff] [blame] | 3587 | status_t AudioPolicyManager::setAllowedCapturePolicy(uid_t uid, audio_flags_mask_t capturePolicy) | 
|  | 3588 | { | 
|  | 3589 | mAllowedCapturePolicies[uid] = capturePolicy; | 
|  | 3590 | return NO_ERROR; | 
|  | 3591 | } | 
|  | 3592 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3593 | // This function checks for the parameters which can be offloaded. | 
|  | 3594 | // This can be enhanced depending on the capability of the DSP and policy | 
|  | 3595 | // of the system. | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3596 | audio_offload_mode_t AudioPolicyManager::getOffloadSupport(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3597 | { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3598 | ALOGV("%s: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3599 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3600 | __func__, offloadInfo.sample_rate, offloadInfo.channel_mask, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3601 | offloadInfo.format, | 
|  | 3602 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
|  | 3603 | offloadInfo.has_video); | 
|  | 3604 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3605 | if (mMasterMono) { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3606 | return AUDIO_OFFLOAD_NOT_SUPPORTED; // no offloading if mono is set. | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3607 | } | 
|  | 3608 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3609 | // Check if offload has been disabled | 
| Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 3610 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3611 | ALOGV("%s: offload disabled by audio.offload.disable", __func__); | 
|  | 3612 | return AUDIO_OFFLOAD_NOT_SUPPORTED; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3613 | } | 
|  | 3614 |  | 
|  | 3615 | // Check if stream type is music, then only allow offload as of now. | 
|  | 3616 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
|  | 3617 | { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3618 | ALOGV("%s: stream_type != MUSIC, returning false", __func__); | 
|  | 3619 | return AUDIO_OFFLOAD_NOT_SUPPORTED; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3620 | } | 
|  | 3621 |  | 
|  | 3622 | //TODO: enable audio offloading with video when ready | 
| Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 3623 | const bool allowOffloadWithVideo = | 
|  | 3624 | property_get_bool("audio.offload.video", false /* default_value */); | 
|  | 3625 | if (offloadInfo.has_video && !allowOffloadWithVideo) { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3626 | ALOGV("%s: has_video == true, returning false", __func__); | 
|  | 3627 | return AUDIO_OFFLOAD_NOT_SUPPORTED; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3628 | } | 
|  | 3629 |  | 
|  | 3630 | //If duration is less than minimum value defined in property, return false | 
| Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 3631 | const int min_duration_secs = property_get_int32( | 
|  | 3632 | "audio.offload.min.duration.secs", -1 /* default_value */); | 
|  | 3633 | if (min_duration_secs >= 0) { | 
|  | 3634 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3635 | ALOGV("%s: Offload denied by duration < audio.offload.min.duration.secs(=%d)", | 
|  | 3636 | __func__, min_duration_secs); | 
|  | 3637 | return AUDIO_OFFLOAD_NOT_SUPPORTED; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3638 | } | 
|  | 3639 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3640 | ALOGV("%s: Offload denied by duration < default min(=%u)", | 
|  | 3641 | __func__, OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
|  | 3642 | return AUDIO_OFFLOAD_NOT_SUPPORTED; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3643 | } | 
|  | 3644 |  | 
|  | 3645 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 3646 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 3647 | // detects there is an active non offloadable effect. | 
|  | 3648 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 3649 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 3650 | // in the background. | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 3651 | if (mEffects.isNonOffloadableEffectEnabled()) { | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3652 | return AUDIO_OFFLOAD_NOT_SUPPORTED; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3653 | } | 
|  | 3654 |  | 
|  | 3655 | // See if there is a profile to support this. | 
|  | 3656 | // AUDIO_DEVICE_NONE | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3657 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3658 | offloadInfo.sample_rate, | 
|  | 3659 | offloadInfo.format, | 
|  | 3660 | offloadInfo.channel_mask, | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3661 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, | 
|  | 3662 | true /* directOnly */); | 
| Eric Laurent | 9436544 | 2021-01-08 18:36:05 +0100 | [diff] [blame] | 3663 | ALOGV("%s: profile %sfound%s", __func__, profile != nullptr ? "" : "NOT ", | 
|  | 3664 | (profile != nullptr && (profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) | 
|  | 3665 | ? ", supports gapless" : ""); | 
| Eric Laurent | 90fe31c | 2020-11-26 20:06:35 +0100 | [diff] [blame] | 3666 | if (profile == nullptr) { | 
|  | 3667 | return AUDIO_OFFLOAD_NOT_SUPPORTED; | 
|  | 3668 | } | 
|  | 3669 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_GAPLESS_OFFLOAD) != 0) { | 
|  | 3670 | return AUDIO_OFFLOAD_GAPLESS_SUPPORTED; | 
|  | 3671 | } | 
|  | 3672 | return AUDIO_OFFLOAD_SUPPORTED; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3673 | } | 
|  | 3674 |  | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3675 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, | 
|  | 3676 | const audio_attributes_t& attributes) { | 
|  | 3677 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; | 
| François Gaffie | 58d4be5 | 2018-11-06 15:30:12 +0100 | [diff] [blame] | 3678 | audio_flags_to_audio_output_flags(attributes.flags, &output_flags); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3679 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3680 | config.sample_rate, | 
|  | 3681 | config.format, | 
|  | 3682 | config.channel_mask, | 
|  | 3683 | output_flags, | 
|  | 3684 | true /* directOnly */); | 
|  | 3685 | ALOGV("%s() profile %sfound with name: %s, " | 
|  | 3686 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", | 
|  | 3687 | __FUNCTION__, profile != 0 ? "" : "NOT ", | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 3688 | (profile != 0 ? profile->getTagName().c_str() : "null"), | 
| Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 3689 | config.sample_rate, config.format, config.channel_mask, output_flags); | 
|  | 3690 | return (profile != 0); | 
|  | 3691 | } | 
|  | 3692 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3693 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
|  | 3694 | audio_port_type_t type, | 
|  | 3695 | unsigned int *num_ports, | 
| jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 3696 | struct audio_port_v7 *ports, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3697 | unsigned int *generation) | 
|  | 3698 | { | 
| jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 3699 | if (num_ports == nullptr || (*num_ports != 0 && ports == nullptr) || | 
|  | 3700 | generation == nullptr) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3701 | return BAD_VALUE; | 
|  | 3702 | } | 
|  | 3703 | 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] | 3704 | if (ports == nullptr) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3705 | *num_ports = 0; | 
|  | 3706 | } | 
|  | 3707 |  | 
|  | 3708 | size_t portsWritten = 0; | 
|  | 3709 | size_t portsMax = *num_ports; | 
|  | 3710 | *num_ports = 0; | 
|  | 3711 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3712 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB | 
|  | 3713 | // as they are used by stub HALs by convention | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3714 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3715 | for (const auto& dev : mAvailableOutputDevices) { | 
|  | 3716 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3717 | continue; | 
|  | 3718 | } | 
|  | 3719 | if (portsWritten < portsMax) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3720 | dev->toAudioPort(&ports[portsWritten++]); | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3721 | } | 
|  | 3722 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3723 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3724 | } | 
|  | 3725 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3726 | for (const auto& dev : mAvailableInputDevices) { | 
|  | 3727 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3728 | continue; | 
|  | 3729 | } | 
|  | 3730 | if (portsWritten < portsMax) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3731 | dev->toAudioPort(&ports[portsWritten++]); | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 3732 | } | 
|  | 3733 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3734 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3735 | } | 
|  | 3736 | } | 
|  | 3737 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
|  | 3738 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 3739 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
|  | 3740 | mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 3741 | } | 
|  | 3742 | *num_ports += mInputs.size(); | 
|  | 3743 | } | 
|  | 3744 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3745 | size_t numOutputs = 0; | 
|  | 3746 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3747 | if (!mOutputs[i]->isDuplicated()) { | 
|  | 3748 | numOutputs++; | 
|  | 3749 | if (portsWritten < portsMax) { | 
|  | 3750 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 3751 | } | 
|  | 3752 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3753 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3754 | *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3755 | } | 
|  | 3756 | } | 
|  | 3757 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3758 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3759 | return NO_ERROR; | 
|  | 3760 | } | 
|  | 3761 |  | 
| jiabin | 19cdba5 | 2020-11-24 11:28:58 -0800 | [diff] [blame] | 3762 | status_t AudioPolicyManager::getAudioPort(struct audio_port_v7 *port) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3763 | { | 
| Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 3764 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { | 
|  | 3765 | return BAD_VALUE; | 
|  | 3766 | } | 
|  | 3767 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); | 
|  | 3768 | if (dev != 0) { | 
|  | 3769 | dev->toAudioPort(port); | 
|  | 3770 | return NO_ERROR; | 
|  | 3771 | } | 
|  | 3772 | dev = mAvailableInputDevices.getDeviceFromId(port->id); | 
|  | 3773 | if (dev != 0) { | 
|  | 3774 | dev->toAudioPort(port); | 
|  | 3775 | return NO_ERROR; | 
|  | 3776 | } | 
|  | 3777 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); | 
|  | 3778 | if (out != 0) { | 
|  | 3779 | out->toAudioPort(port); | 
|  | 3780 | return NO_ERROR; | 
|  | 3781 | } | 
|  | 3782 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); | 
|  | 3783 | if (in != 0) { | 
|  | 3784 | in->toAudioPort(port); | 
|  | 3785 | return NO_ERROR; | 
|  | 3786 | } | 
|  | 3787 | return BAD_VALUE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3788 | } | 
|  | 3789 |  | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3790 | status_t AudioPolicyManager::createAudioPatchInternal(const struct audio_patch *patch, | 
|  | 3791 | audio_patch_handle_t *handle, | 
|  | 3792 | uid_t uid, uint32_t delayMs, | 
|  | 3793 | const sp<SourceClientDescriptor>& sourceDesc) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3794 | { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3795 | ALOGV("%s", __func__); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3796 | if (handle == NULL || patch == NULL) { | 
|  | 3797 | return BAD_VALUE; | 
|  | 3798 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3799 | 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] | 3800 |  | 
| Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 3801 | if (!audio_patch_is_valid(patch)) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3802 | return BAD_VALUE; | 
|  | 3803 | } | 
|  | 3804 | // only one source per audio patch supported for now | 
|  | 3805 | if (patch->num_sources > 1) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3806 | return INVALID_OPERATION; | 
|  | 3807 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3808 |  | 
|  | 3809 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3810 | return INVALID_OPERATION; | 
|  | 3811 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3812 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 3813 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { | 
|  | 3814 | return INVALID_OPERATION; | 
|  | 3815 | } | 
|  | 3816 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3817 |  | 
|  | 3818 | sp<AudioPatch> patchDesc; | 
|  | 3819 | ssize_t index = mAudioPatches.indexOfKey(*handle); | 
|  | 3820 |  | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3821 | ALOGV("%s source id %d role %d type %d", __func__, patch->sources[0].id, | 
|  | 3822 | patch->sources[0].role, | 
|  | 3823 | patch->sources[0].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3824 | #if LOG_NDEBUG == 0 | 
|  | 3825 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3826 | ALOGV("%s sink %zu: id %d role %d type %d", __func__ ,i, patch->sinks[i].id, | 
|  | 3827 | patch->sinks[i].role, | 
|  | 3828 | patch->sinks[i].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3829 | } | 
|  | 3830 | #endif | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3831 |  | 
|  | 3832 | if (index >= 0) { | 
|  | 3833 | patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3834 | ALOGV("%s mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 3835 | __func__, mUidCached, patchDesc->getUid(), uid); | 
|  | 3836 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3837 | return INVALID_OPERATION; | 
|  | 3838 | } | 
|  | 3839 | } else { | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 3840 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3841 | } | 
|  | 3842 |  | 
|  | 3843 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3844 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3845 | if (outputDesc == NULL) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3846 | 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] | 3847 | return BAD_VALUE; | 
|  | 3848 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3849 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
|  | 3850 | outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3851 | if (patchDesc != 0) { | 
|  | 3852 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3853 | ALOGV("%s source id differs for patch current id %d new id %d", | 
|  | 3854 | __func__, patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3855 | return BAD_VALUE; | 
|  | 3856 | } | 
|  | 3857 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3858 | DeviceVector devices; | 
|  | 3859 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 3860 | // Only support mix to devices connection | 
|  | 3861 | // TODO add support for mix to mix connection | 
|  | 3862 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3863 | ALOGV("%s source mix but sink is not a device", __func__); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3864 | return INVALID_OPERATION; | 
|  | 3865 | } | 
|  | 3866 | sp<DeviceDescriptor> devDesc = | 
|  | 3867 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 3868 | if (devDesc == 0) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3869 | 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] | 3870 | return BAD_VALUE; | 
|  | 3871 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3872 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3873 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3874 | patch->sources[0].sample_rate, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3875 | NULL,  // updatedSamplingRate | 
|  | 3876 | patch->sources[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3877 | NULL,  // updatedFormat | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3878 | patch->sources[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3879 | NULL,  // updatedChannelMask | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3880 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3881 | ALOGV("%s profile not supported for device %08x", __func__, devDesc->type()); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3882 | return INVALID_OPERATION; | 
|  | 3883 | } | 
|  | 3884 | devices.add(devDesc); | 
|  | 3885 | } | 
|  | 3886 | if (devices.size() == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3887 | return INVALID_OPERATION; | 
|  | 3888 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3889 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3890 | // TODO: reconfigure output format and channels here | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3891 | ALOGV("%s setting device %s on output %d", | 
|  | 3892 | __func__, dumpDeviceTypes(devices.types()).c_str(), outputDesc->mIoHandle); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3893 | setOutputDevices(outputDesc, devices, true, 0, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3894 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 3895 | if (index >= 0) { | 
|  | 3896 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3897 | ALOGW("%s setOutputDevice() did not reuse the patch provided", __func__); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3898 | } | 
|  | 3899 | patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3900 | patchDesc->setUid(uid); | 
|  | 3901 | ALOGV("%s success", __func__); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3902 | } else { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3903 | ALOGW("%s setOutputDevice() failed to create a patch", __func__); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3904 | return INVALID_OPERATION; | 
|  | 3905 | } | 
|  | 3906 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3907 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 3908 | // input device to input mix connection | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3909 | // only one sink supported when connecting an input device to a mix | 
|  | 3910 | if (patch->num_sinks > 1) { | 
|  | 3911 | return INVALID_OPERATION; | 
|  | 3912 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3913 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3914 | if (inputDesc == NULL) { | 
|  | 3915 | return BAD_VALUE; | 
|  | 3916 | } | 
|  | 3917 | if (patchDesc != 0) { | 
|  | 3918 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 3919 | return BAD_VALUE; | 
|  | 3920 | } | 
|  | 3921 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3922 | sp<DeviceDescriptor> device = | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3923 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3924 | if (device == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3925 | return BAD_VALUE; | 
|  | 3926 | } | 
|  | 3927 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3928 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3929 | patch->sinks[0].sample_rate, | 
|  | 3930 | NULL, /*updatedSampleRate*/ | 
|  | 3931 | patch->sinks[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3932 | NULL, /*updatedFormat*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3933 | patch->sinks[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3934 | NULL, /*updatedChannelMask*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3935 | // FIXME for the parameter type, | 
|  | 3936 | // and the NONE | 
|  | 3937 | (audio_output_flags_t) | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3938 | AUDIO_INPUT_FLAG_NONE)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3939 | return INVALID_OPERATION; | 
|  | 3940 | } | 
|  | 3941 | // TODO: reconfigure output format and channels here | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3942 | ALOGV("%s setting device %s on output %d", __func__, | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3943 | device->toString().c_str(), inputDesc->mIoHandle); | 
|  | 3944 | setInputDevice(inputDesc->mIoHandle, device, true, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3945 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 3946 | if (index >= 0) { | 
|  | 3947 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3948 | ALOGW("%s setInputDevice() did not reuse the patch provided", __func__); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3949 | } | 
|  | 3950 | patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3951 | patchDesc->setUid(uid); | 
|  | 3952 | ALOGV("%s success", __func__); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3953 | } else { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3954 | ALOGW("%s setInputDevice() failed to create a patch", __func__); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3955 | return INVALID_OPERATION; | 
|  | 3956 | } | 
|  | 3957 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3958 | // device to device connection | 
|  | 3959 | if (patchDesc != 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3960 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3961 | return BAD_VALUE; | 
|  | 3962 | } | 
|  | 3963 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3964 | sp<DeviceDescriptor> srcDevice = | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3965 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3966 | if (srcDevice == 0) { | 
| Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3967 | return BAD_VALUE; | 
|  | 3968 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3969 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3970 | //update source and sink with our own data as the data passed in the patch may | 
|  | 3971 | // be incomplete. | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3972 | PatchBuilder patchBuilder; | 
|  | 3973 | audio_port_config sourcePortConfig = {}; | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 3974 |  | 
|  | 3975 | // if first sink is to MSD, establish single MSD patch | 
|  | 3976 | if (getMsdAudioOutDevices().contains( | 
|  | 3977 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[0].id))) { | 
|  | 3978 | ALOGV("%s patching to MSD", __FUNCTION__); | 
|  | 3979 | patchBuilder = buildMsdPatch(false /*msdIsSource*/, srcDevice); | 
|  | 3980 | goto installPatch; | 
|  | 3981 | } | 
|  | 3982 |  | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3983 | srcDevice->toAudioPortConfig(&sourcePortConfig, &patch->sources[0]); | 
|  | 3984 | patchBuilder.addSource(sourcePortConfig); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3985 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3986 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 3987 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3988 | ALOGV("%s source device but one sink is not a device", __func__); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3989 | return INVALID_OPERATION; | 
|  | 3990 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3991 | sp<DeviceDescriptor> sinkDevice = | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3992 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3993 | if (sinkDevice == 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3994 | return BAD_VALUE; | 
|  | 3995 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 3996 | audio_port_config sinkPortConfig = {}; | 
|  | 3997 | sinkDevice->toAudioPortConfig(&sinkPortConfig, &patch->sinks[i]); | 
|  | 3998 | patchBuilder.addSink(sinkPortConfig); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3999 |  | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4000 | // Whatever Sw or Hw bridge, we do attach an SwOutput to an Audio Source for | 
|  | 4001 | // volume management purpose (tracking activity) | 
|  | 4002 | // In case of Hw bridge, it is a Work Around. The mixPort used is the one declared | 
|  | 4003 | // in config XML to reach the sink so that is can be declared as available. | 
|  | 4004 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 4005 | sp<SwAudioOutputDescriptor> outputDesc = nullptr; | 
|  | 4006 | if (sourceDesc != nullptr) { | 
|  | 4007 | // take care of dynamic routing for SwOutput selection, | 
|  | 4008 | audio_attributes_t attributes = sourceDesc->attributes(); | 
|  | 4009 | audio_stream_type_t stream = sourceDesc->stream(); | 
|  | 4010 | audio_attributes_t resultAttr; | 
|  | 4011 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 4012 | config.sample_rate = sourceDesc->config().sample_rate; | 
|  | 4013 | config.channel_mask = sourceDesc->config().channel_mask; | 
|  | 4014 | config.format = sourceDesc->config().format; | 
|  | 4015 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; | 
|  | 4016 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; | 
|  | 4017 | bool isRequestedDeviceForExclusiveUse = false; | 
|  | 4018 | output_type_t outputType; | 
|  | 4019 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, &attributes, | 
|  | 4020 | &stream, sourceDesc->uid(), &config, &flags, | 
|  | 4021 | &selectedDeviceId, &isRequestedDeviceForExclusiveUse, | 
|  | 4022 | nullptr, &outputType); | 
|  | 4023 | if (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 4024 | ALOGV("%s no output for device %s", | 
|  | 4025 | __FUNCTION__, sinkDevice->toString().c_str()); | 
|  | 4026 | return INVALID_OPERATION; | 
|  | 4027 | } | 
|  | 4028 | outputDesc = mOutputs.valueFor(output); | 
|  | 4029 | if (outputDesc->isDuplicated()) { | 
|  | 4030 | ALOGE("%s output is duplicated", __func__); | 
|  | 4031 | return INVALID_OPERATION; | 
|  | 4032 | } | 
|  | 4033 | sourceDesc->setSwOutput(outputDesc); | 
|  | 4034 | } | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4035 | // create a software bridge in PatchPanel if: | 
| Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 4036 | // - source and sink devices are on different HW modules OR | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4037 | // - audio HAL version is < 3.0 | 
| Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 4038 | // - 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] | 4039 | // - called from startAudioSource (aka sourceDesc != nullptr) and source device does | 
|  | 4040 | //   not have a gain controller | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4041 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || | 
|  | 4042 | (srcDevice->getModuleVersionMajor() < 3) || | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4043 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice) || | 
|  | 4044 | (sourceDesc != nullptr && | 
|  | 4045 | srcDevice->getAudioPort()->getGains().size() == 0)) { | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 4046 | // support only one sink device for now to simplify output selection logic | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4047 | if (patch->num_sinks > 1) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4048 | return INVALID_OPERATION; | 
|  | 4049 | } | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4050 | if (sourceDesc == nullptr) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4051 | SortedVector<audio_io_handle_t> outputs = | 
|  | 4052 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); | 
|  | 4053 | // if the sink device is reachable via an opened output stream, request to | 
|  | 4054 | // go via this output stream by adding a second source to the patch | 
|  | 4055 | // description | 
|  | 4056 | output = selectOutput(outputs); | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4057 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 4058 | outputDesc = mOutputs.valueFor(output); | 
|  | 4059 | if (outputDesc->isDuplicated()) { | 
|  | 4060 | ALOGV("%s output for device %s is duplicated", | 
|  | 4061 | __FUNCTION__, sinkDevice->toString().c_str()); | 
|  | 4062 | return INVALID_OPERATION; | 
|  | 4063 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4064 | } | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 4065 | } | 
|  | 4066 | if (outputDesc != nullptr) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4067 | audio_port_config srcMixPortConfig = {}; | 
|  | 4068 | outputDesc->toAudioPortConfig(&srcMixPortConfig, &patch->sources[0]); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4069 | // for volume control, we may need a valid stream | 
|  | 4070 | srcMixPortConfig.ext.mix.usecase.stream = sourceDesc != nullptr ? | 
|  | 4071 | sourceDesc->stream() : AUDIO_STREAM_PATCH; | 
|  | 4072 | patchBuilder.addSource(srcMixPortConfig); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 4073 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4074 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4075 | } | 
|  | 4076 | // TODO: check from routing capabilities in config file and other conflicting patches | 
|  | 4077 |  | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 4078 | installPatch: | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4079 | status_t status = installPatch( | 
|  | 4080 | __func__, index, handle, patchBuilder.patch(), delayMs, uid, &patchDesc); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 4081 | if (status != NO_ERROR) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4082 | 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] | 4083 | return INVALID_OPERATION; | 
|  | 4084 | } | 
|  | 4085 | } else { | 
|  | 4086 | return BAD_VALUE; | 
|  | 4087 | } | 
|  | 4088 | } else { | 
|  | 4089 | return BAD_VALUE; | 
|  | 4090 | } | 
|  | 4091 | return NO_ERROR; | 
|  | 4092 | } | 
|  | 4093 |  | 
|  | 4094 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
|  | 4095 | uid_t uid) | 
|  | 4096 | { | 
|  | 4097 | ALOGV("releaseAudioPatch() patch %d", handle); | 
|  | 4098 |  | 
|  | 4099 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 4100 |  | 
|  | 4101 | if (index < 0) { | 
|  | 4102 | return BAD_VALUE; | 
|  | 4103 | } | 
|  | 4104 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4105 | ALOGV("%s() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 4106 | __func__, mUidCached, patchDesc->getUid(), uid); | 
|  | 4107 | if (patchDesc->getUid() != mUidCached && uid != patchDesc->getUid()) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4108 | return INVALID_OPERATION; | 
|  | 4109 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4110 | return releaseAudioPatchInternal(handle); | 
|  | 4111 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4112 |  | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4113 | status_t AudioPolicyManager::releaseAudioPatchInternal(audio_patch_handle_t handle, | 
|  | 4114 | uint32_t delayMs) | 
|  | 4115 | { | 
|  | 4116 | ALOGV("%s patch %d", __func__, handle); | 
|  | 4117 | if (mAudioPatches.indexOfKey(handle) < 0) { | 
|  | 4118 | ALOGE("%s: no patch found with handle=%d", __func__, handle); | 
|  | 4119 | return BAD_VALUE; | 
|  | 4120 | } | 
|  | 4121 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4122 | struct audio_patch *patch = &patchDesc->mPatch; | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4123 | patchDesc->setUid(mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4124 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4125 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4126 | if (outputDesc == NULL) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4127 | 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] | 4128 | return BAD_VALUE; | 
|  | 4129 | } | 
|  | 4130 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4131 | setOutputDevices(outputDesc, | 
|  | 4132 | getNewOutputDevices(outputDesc, true /*fromCache*/), | 
|  | 4133 | true, | 
|  | 4134 | 0, | 
|  | 4135 | NULL); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4136 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 4137 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4138 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4139 | if (inputDesc == NULL) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4140 | 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] | 4141 | return BAD_VALUE; | 
|  | 4142 | } | 
|  | 4143 | setInputDevice(inputDesc->mIoHandle, | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4144 | getNewInputDevice(inputDesc), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4145 | true, | 
|  | 4146 | NULL); | 
|  | 4147 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4148 | status_t status = | 
|  | 4149 | mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); | 
|  | 4150 | ALOGV("%s patch panel returned %d patchHandle %d", | 
|  | 4151 | __func__, status, patchDesc->getAfHandle()); | 
|  | 4152 | removeAudioPatch(patchDesc->getHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4153 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4154 | mpClientInterface->onAudioPatchListUpdate(); | 
| Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4155 | // SW Bridge | 
|  | 4156 | if (patch->num_sources > 1 && patch->sources[1].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 4157 | sp<SwAudioOutputDescriptor> outputDesc = | 
|  | 4158 | mOutputs.getOutputFromId(patch->sources[1].id); | 
|  | 4159 | if (outputDesc == NULL) { | 
| Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 4160 | ALOGW("%s output not found for id %d", __func__, patch->sources[0].id); | 
|  | 4161 | // releaseOutput has already called closeOuput in case of direct output | 
|  | 4162 | return NO_ERROR; | 
| Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4163 | } | 
| Francois Gaffie | 8e54454 | 2020-05-11 14:12:53 +0200 | [diff] [blame] | 4164 | if (patchDesc->getHandle() != outputDesc->getPatchHandle()) { | 
|  | 4165 | // force SwOutput patch removal as AF counter part patch has already gone. | 
|  | 4166 | ALOGV("%s reset patch handle on Output as different from SWBridge", __func__); | 
|  | 4167 | removeAudioPatch(outputDesc->getPatchHandle()); | 
|  | 4168 | } | 
| Francois Gaffie | 7feb854 | 2020-04-06 17:39:47 +0200 | [diff] [blame] | 4169 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
|  | 4170 | setOutputDevices(outputDesc, | 
|  | 4171 | getNewOutputDevices(outputDesc, true /*fromCache*/), | 
|  | 4172 | true, /*force*/ | 
|  | 4173 | 0, | 
|  | 4174 | NULL); | 
|  | 4175 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4176 | } else { | 
|  | 4177 | return BAD_VALUE; | 
|  | 4178 | } | 
|  | 4179 | } else { | 
|  | 4180 | return BAD_VALUE; | 
|  | 4181 | } | 
|  | 4182 | return NO_ERROR; | 
|  | 4183 | } | 
|  | 4184 |  | 
|  | 4185 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
|  | 4186 | struct audio_patch *patches, | 
|  | 4187 | unsigned int *generation) | 
|  | 4188 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4189 | if (generation == NULL) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4190 | return BAD_VALUE; | 
|  | 4191 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4192 | *generation = curAudioPortGeneration(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4193 | return mAudioPatches.listAudioPatches(num_patches, patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4194 | } | 
|  | 4195 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4196 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4197 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4198 | ALOGV("setAudioPortConfig()"); | 
|  | 4199 |  | 
|  | 4200 | if (config == NULL) { | 
|  | 4201 | return BAD_VALUE; | 
|  | 4202 | } | 
|  | 4203 | ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
|  | 4204 | // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4205 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
|  | 4206 | return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4207 | } | 
|  | 4208 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4209 | sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4210 | if (config->type == AUDIO_PORT_TYPE_MIX) { | 
|  | 4211 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4212 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4213 | if (outputDesc == NULL) { | 
|  | 4214 | return BAD_VALUE; | 
|  | 4215 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 4216 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 4217 | "setAudioPortConfig() called on duplicated output %d", | 
|  | 4218 | outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4219 | audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4220 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4221 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4222 | if (inputDesc == NULL) { | 
|  | 4223 | return BAD_VALUE; | 
|  | 4224 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4225 | audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4226 | } else { | 
|  | 4227 | return BAD_VALUE; | 
|  | 4228 | } | 
|  | 4229 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 4230 | sp<DeviceDescriptor> deviceDesc; | 
|  | 4231 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
|  | 4232 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
|  | 4233 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
|  | 4234 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
|  | 4235 | } else { | 
|  | 4236 | return BAD_VALUE; | 
|  | 4237 | } | 
|  | 4238 | if (deviceDesc == NULL) { | 
|  | 4239 | return BAD_VALUE; | 
|  | 4240 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4241 | audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4242 | } else { | 
|  | 4243 | return BAD_VALUE; | 
|  | 4244 | } | 
|  | 4245 |  | 
| Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4246 | struct audio_port_config backupConfig = {}; | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4247 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
|  | 4248 | if (status == NO_ERROR) { | 
| Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 4249 | struct audio_port_config newConfig = {}; | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4250 | audioPortConfig->toAudioPortConfig(&newConfig, config); | 
|  | 4251 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4252 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 4253 | if (status != NO_ERROR) { | 
|  | 4254 | audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4255 | } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 4256 |  | 
|  | 4257 | return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4258 | } | 
|  | 4259 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4260 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) | 
|  | 4261 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4262 | clearAudioSources(uid); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4263 | clearAudioPatches(uid); | 
|  | 4264 | clearSessionRoutes(uid); | 
|  | 4265 | } | 
|  | 4266 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4267 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
|  | 4268 | { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4269 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4270 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4271 | if (patchDesc->getUid() == uid) { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 4272 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4273 | } | 
|  | 4274 | } | 
|  | 4275 | } | 
|  | 4276 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4277 | void AudioPolicyManager::checkStrategyRoute(product_strategy_t ps, audio_io_handle_t ouptutToSkip) | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4278 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4279 | // Take the first attributes following the product strategy as it is used to retrieve the routed | 
|  | 4280 | // device. All attributes wihin a strategy follows the same "routing strategy" | 
|  | 4281 | auto attributes = mEngine->getAllAttributesForProductStrategy(ps).front(); | 
|  | 4282 | DeviceVector devices = mEngine->getOutputDevicesForAttributes(attributes, nullptr, false); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4283 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4284 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
|  | 4285 | if (mOutputs.keyAt(j) == ouptutToSkip) { | 
|  | 4286 | continue; | 
|  | 4287 | } | 
|  | 4288 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4289 | if (!outputDesc->isStrategyActive(ps)) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4290 | continue; | 
|  | 4291 | } | 
|  | 4292 | // If the default device for this strategy is on another output mix, | 
|  | 4293 | // invalidate all tracks in this strategy to force re connection. | 
|  | 4294 | // Otherwise select new device on the output mix. | 
|  | 4295 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4296 | for (auto stream : mEngine->getStreamTypesForProductStrategy(ps)) { | 
|  | 4297 | mpClientInterface->invalidateStream(stream); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4298 | } | 
|  | 4299 | } else { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4300 | setOutputDevices( | 
|  | 4301 | outputDesc, getNewOutputDevices(outputDesc, false /*fromCache*/), false); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4302 | } | 
|  | 4303 | } | 
|  | 4304 | } | 
|  | 4305 |  | 
|  | 4306 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) | 
|  | 4307 | { | 
|  | 4308 | // remove output routes associated with this uid | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4309 | std::vector<product_strategy_t> affectedStrategies; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4310 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 4311 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4312 | for (const auto& client : outputDesc->getClientIterable()) { | 
|  | 4313 | if (client->hasPreferredDevice() && client->uid() == uid) { | 
|  | 4314 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4315 | auto clientStrategy = client->strategy(); | 
|  | 4316 | if (std::find(begin(affectedStrategies), end(affectedStrategies), clientStrategy) != | 
|  | 4317 | end(affectedStrategies)) { | 
|  | 4318 | continue; | 
|  | 4319 | } | 
|  | 4320 | affectedStrategies.push_back(client->strategy()); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4321 | } | 
|  | 4322 | } | 
|  | 4323 | } | 
|  | 4324 | // reroute outputs if necessary | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4325 | for (const auto& strategy : affectedStrategies) { | 
|  | 4326 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4327 | } | 
|  | 4328 |  | 
|  | 4329 | // remove input routes associated with this uid | 
|  | 4330 | SortedVector<audio_source_t> affectedSources; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4331 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 4332 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); | 
| Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 4333 | for (const auto& client : inputDesc->getClientIterable()) { | 
|  | 4334 | if (client->hasPreferredDevice() && client->uid() == uid) { | 
|  | 4335 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); | 
|  | 4336 | affectedSources.add(client->source()); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4337 | } | 
|  | 4338 | } | 
|  | 4339 | } | 
|  | 4340 | // reroute inputs if necessary | 
|  | 4341 | SortedVector<audio_io_handle_t> inputsToClose; | 
|  | 4342 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 4343 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 4344 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4345 | inputsToClose.add(inputDesc->mIoHandle); | 
|  | 4346 | } | 
|  | 4347 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4348 | for (const auto& input : inputsToClose) { | 
|  | 4349 | closeInput(input); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4350 | } | 
|  | 4351 | } | 
|  | 4352 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4353 | void AudioPolicyManager::clearAudioSources(uid_t uid) | 
|  | 4354 | { | 
|  | 4355 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4356 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 4357 | if (sourceDesc->uid() == uid) { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4358 | stopAudioSource(mAudioSources.keyAt(i)); | 
|  | 4359 | } | 
|  | 4360 | } | 
|  | 4361 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4362 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4363 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, | 
|  | 4364 | audio_io_handle_t *ioHandle, | 
|  | 4365 | audio_devices_t *device) | 
|  | 4366 | { | 
| Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 4367 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
|  | 4368 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4369 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4370 | *device = mEngine->getInputDeviceForAttributes(attr)->type(); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4371 |  | 
| François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 4372 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4373 | } | 
|  | 4374 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4375 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4376 | const audio_attributes_t *attributes, | 
|  | 4377 | audio_port_handle_t *portId, | 
|  | 4378 | uid_t uid) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4379 | { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4380 | ALOGV("%s", __FUNCTION__); | 
|  | 4381 | *portId = AUDIO_PORT_HANDLE_NONE; | 
|  | 4382 |  | 
|  | 4383 | if (source == NULL || attributes == NULL || portId == NULL) { | 
|  | 4384 | ALOGW("%s invalid argument: source %p attributes %p handle %p", | 
|  | 4385 | __FUNCTION__, source, attributes, portId); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4386 | return BAD_VALUE; | 
|  | 4387 | } | 
|  | 4388 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4389 | if (source->role != AUDIO_PORT_ROLE_SOURCE || | 
|  | 4390 | source->type != AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4391 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", | 
|  | 4392 | __FUNCTION__, source->role, source->type); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4393 | return INVALID_OPERATION; | 
|  | 4394 | } | 
|  | 4395 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4396 | sp<DeviceDescriptor> srcDevice = | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4397 | mAvailableInputDevices.getDevice(source->ext.device.type, | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4398 | String8(source->ext.device.address), | 
|  | 4399 | AUDIO_FORMAT_DEFAULT); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4400 | if (srcDevice == 0) { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4401 | 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] | 4402 | return BAD_VALUE; | 
|  | 4403 | } | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4404 |  | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 4405 | *portId = PolicyAudioPort::getNextUniqueId(); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4406 |  | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4407 | sp<SourceClientDescriptor> sourceDesc = | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4408 | new SourceClientDescriptor(*portId, uid, *attributes, *source, srcDevice, | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 4409 | mEngine->getStreamTypeForAttributes(*attributes), | 
|  | 4410 | mEngine->getProductStrategyForAttributes(*attributes), | 
|  | 4411 | toVolumeSource(*attributes)); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4412 |  | 
|  | 4413 | status_t status = connectAudioSource(sourceDesc); | 
|  | 4414 | if (status == NO_ERROR) { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4415 | mAudioSources.add(*portId, sourceDesc); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4416 | } | 
|  | 4417 | return status; | 
|  | 4418 | } | 
|  | 4419 |  | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4420 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4421 | { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4422 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4423 |  | 
|  | 4424 | // make sure we only have one patch per source. | 
|  | 4425 | disconnectAudioSource(sourceDesc); | 
|  | 4426 |  | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4427 | audio_attributes_t attributes = sourceDesc->attributes(); | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4428 | // May the device (dynamic) have been disconnected/reconnected, id has changed. | 
|  | 4429 | sp<DeviceDescriptor> srcDevice = mAvailableInputDevices.getDevice( | 
|  | 4430 | sourceDesc->srcDevice()->type(), | 
|  | 4431 | String8(sourceDesc->srcDevice()->address().c_str()), | 
|  | 4432 | AUDIO_FORMAT_DEFAULT); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4433 | DeviceVector sinkDevices = | 
| Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 4434 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false /*fromCache*/); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4435 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for attributes"); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4436 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4437 | if (!mAvailableOutputDevices.contains(sinkDevice)) { | 
|  | 4438 | ALOGE("%s Device %s not available", __func__, sinkDevice->toString().c_str()); | 
|  | 4439 | return INVALID_OPERATION; | 
|  | 4440 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4441 | PatchBuilder patchBuilder; | 
|  | 4442 | patchBuilder.addSink(sinkDevice).addSource(srcDevice); | 
|  | 4443 | audio_patch_handle_t handle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4444 | status_t status = | 
|  | 4445 | createAudioPatchInternal(patchBuilder.patch(), &handle, mUidCached, 0, sourceDesc); | 
|  | 4446 | if (status != NO_ERROR || mAudioPatches.indexOfKey(handle) < 0) { | 
|  | 4447 | ALOGW("%s patch panel could not connect device patch, error %d", __func__, status); | 
|  | 4448 | return INVALID_OPERATION; | 
|  | 4449 | } | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4450 | sourceDesc->connect(handle, sinkDevice); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4451 | // SW Bridge? (@todo: HW bridge, keep track of HwOutput for device selection "reconsideration") | 
|  | 4452 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); | 
|  | 4453 | if (swOutput != 0) { | 
|  | 4454 | status = swOutput->start(); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4455 | if (status != NO_ERROR) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4456 | goto FailureSourceAdded; | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4457 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4458 | if (swOutput->getClient(sourceDesc->portId()) != nullptr) { | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 4459 | ALOGW("%s source portId has already been attached to outputDesc", __func__); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4460 | goto FailureReleasePatch; | 
| Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 4461 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4462 | swOutput->addClient(sourceDesc); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4463 | uint32_t delayMs = 0; | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4464 | status = startSource(swOutput, sourceDesc, &delayMs); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4465 | if (status != NO_ERROR) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4466 | ALOGW("%s failed to start source, error %d", __FUNCTION__, status); | 
|  | 4467 | goto FailureSourceActive; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4468 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4469 | if (delayMs != 0) { | 
|  | 4470 | usleep(delayMs * 1000); | 
|  | 4471 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4472 | } else { | 
|  | 4473 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); | 
|  | 4474 | if (hwOutputDesc != 0) { | 
|  | 4475 | //   create Hwoutput and add to mHwOutputs | 
|  | 4476 | } else { | 
|  | 4477 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); | 
|  | 4478 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4479 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4480 | return NO_ERROR; | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4481 |  | 
|  | 4482 | FailureSourceActive: | 
|  | 4483 | swOutput->stop(); | 
|  | 4484 | releaseOutput(sourceDesc->portId()); | 
|  | 4485 | FailureSourceAdded: | 
|  | 4486 | sourceDesc->setSwOutput(nullptr); | 
|  | 4487 | FailureReleasePatch: | 
|  | 4488 | releaseAudioPatchInternal(handle); | 
|  | 4489 | return INVALID_OPERATION; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4490 | } | 
|  | 4491 |  | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4492 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4493 | { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4494 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); | 
|  | 4495 | ALOGV("%s port ID %d", __FUNCTION__, portId); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4496 | if (sourceDesc == 0) { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4497 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4498 | return BAD_VALUE; | 
|  | 4499 | } | 
|  | 4500 | status_t status = disconnectAudioSource(sourceDesc); | 
|  | 4501 |  | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4502 | mAudioSources.removeItem(portId); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4503 | return status; | 
|  | 4504 | } | 
|  | 4505 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4506 | status_t AudioPolicyManager::setMasterMono(bool mono) | 
|  | 4507 | { | 
|  | 4508 | if (mMasterMono == mono) { | 
|  | 4509 | return NO_ERROR; | 
|  | 4510 | } | 
|  | 4511 | mMasterMono = mono; | 
|  | 4512 | // if enabling mono we close all offloaded devices, which will invalidate the | 
|  | 4513 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible | 
|  | 4514 | // for recreating the new AudioTrack as non-offloaded PCM. | 
|  | 4515 | // | 
|  | 4516 | // If disabling mono, we leave all tracks as is: we don't know which clients | 
|  | 4517 | // and tracks are able to be recreated as offloaded. The next "song" should | 
|  | 4518 | // play back offloaded. | 
|  | 4519 | if (mMasterMono) { | 
|  | 4520 | Vector<audio_io_handle_t> offloaded; | 
|  | 4521 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 4522 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 4523 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
|  | 4524 | offloaded.push(desc->mIoHandle); | 
|  | 4525 | } | 
|  | 4526 | } | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4527 | for (const auto& handle : offloaded) { | 
|  | 4528 | closeOutput(handle); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4529 | } | 
|  | 4530 | } | 
|  | 4531 | // update master mono for all remaining outputs | 
|  | 4532 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 4533 | updateMono(mOutputs.keyAt(i)); | 
|  | 4534 | } | 
|  | 4535 | return NO_ERROR; | 
|  | 4536 | } | 
|  | 4537 |  | 
|  | 4538 | status_t AudioPolicyManager::getMasterMono(bool *mono) | 
|  | 4539 | { | 
|  | 4540 | *mono = mMasterMono; | 
|  | 4541 | return NO_ERROR; | 
|  | 4542 | } | 
|  | 4543 |  | 
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 4544 | float AudioPolicyManager::getStreamVolumeDB( | 
|  | 4545 | audio_stream_type_t stream, int index, audio_devices_t device) | 
|  | 4546 | { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 4547 | return computeVolume(getVolumeCurves(stream), toVolumeSource(stream), index, {device}); | 
| Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 4548 | } | 
|  | 4549 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4550 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, | 
|  | 4551 | audio_format_t *surroundFormats, | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4552 | bool *surroundFormatsEnabled) | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4553 | { | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4554 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && | 
|  | 4555 | (surroundFormats == nullptr || surroundFormatsEnabled == nullptr))) { | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4556 | return BAD_VALUE; | 
|  | 4557 | } | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4558 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", | 
|  | 4559 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4560 |  | 
|  | 4561 | size_t formatsWritten = 0; | 
|  | 4562 | size_t formatsMax = *numSurroundFormats; | 
| Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 4563 |  | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4564 | *numSurroundFormats = mConfig.getSurroundFormats().size(); | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4565 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 4566 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4567 | for (const auto& format: mConfig.getSurroundFormats()) { | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4568 | if (formatsWritten < formatsMax) { | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4569 | surroundFormats[formatsWritten] = format.first; | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4570 | bool formatEnabled = true; | 
|  | 4571 | switch (forceUse) { | 
|  | 4572 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4573 | formatEnabled = mManualSurroundFormats.count(format.first) != 0; | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4574 | break; | 
|  | 4575 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: | 
|  | 4576 | formatEnabled = false; | 
|  | 4577 | break; | 
|  | 4578 | default: // AUTO or ALWAYS => true | 
|  | 4579 | break; | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4580 | } | 
|  | 4581 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; | 
|  | 4582 | } | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4583 | } | 
|  | 4584 | return NO_ERROR; | 
|  | 4585 | } | 
|  | 4586 |  | 
| Kriti Dang | 6537def | 2021-03-02 13:46:59 +0100 | [diff] [blame] | 4587 | status_t AudioPolicyManager::getReportedSurroundFormats(unsigned int *numSurroundFormats, | 
|  | 4588 | audio_format_t *surroundFormats) { | 
|  | 4589 | if (numSurroundFormats == nullptr || (*numSurroundFormats != 0 && surroundFormats == nullptr)) { | 
|  | 4590 | return BAD_VALUE; | 
|  | 4591 | } | 
|  | 4592 | ALOGV("%s() numSurroundFormats %d surroundFormats %p", | 
|  | 4593 | __func__, *numSurroundFormats, surroundFormats); | 
|  | 4594 |  | 
|  | 4595 | size_t formatsWritten = 0; | 
|  | 4596 | size_t formatsMax = *numSurroundFormats; | 
|  | 4597 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only | 
|  | 4598 |  | 
|  | 4599 | // Return formats from all device profiles that have already been resolved by | 
|  | 4600 | // checkOutputsForDevice(). | 
|  | 4601 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
|  | 4602 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; | 
|  | 4603 | audio_devices_t deviceType = device->type(); | 
|  | 4604 | // Enabling/disabling formats are applied to only HDMI devices. So, this function | 
|  | 4605 | // returns formats reported by HDMI devices. | 
|  | 4606 | if (deviceType != AUDIO_DEVICE_OUT_HDMI) { | 
|  | 4607 | continue; | 
|  | 4608 | } | 
|  | 4609 | // Formats reported by sink devices | 
|  | 4610 | std::unordered_set<audio_format_t> formatset; | 
|  | 4611 | if (auto it = mReportedFormatsMap.find(device); it != mReportedFormatsMap.end()) { | 
|  | 4612 | formatset.insert(it->second.begin(), it->second.end()); | 
|  | 4613 | } | 
|  | 4614 |  | 
|  | 4615 | // Formats hard-coded in the in policy configuration file (if any). | 
|  | 4616 | FormatVector encodedFormats = device->encodedFormats(); | 
|  | 4617 | formatset.insert(encodedFormats.begin(), encodedFormats.end()); | 
|  | 4618 | // Filter the formats which are supported by the vendor hardware. | 
|  | 4619 | for (auto it = formatset.begin(); it != formatset.end(); ++it) { | 
|  | 4620 | if (mConfig.getSurroundFormats().count(*it) != 0) { | 
|  | 4621 | formats.insert(*it); | 
|  | 4622 | } else { | 
|  | 4623 | for (const auto& pair : mConfig.getSurroundFormats()) { | 
|  | 4624 | if (pair.second.count(*it) != 0) { | 
|  | 4625 | formats.insert(pair.first); | 
|  | 4626 | break; | 
|  | 4627 | } | 
|  | 4628 | } | 
|  | 4629 | } | 
|  | 4630 | } | 
|  | 4631 | } | 
|  | 4632 | *numSurroundFormats = formats.size(); | 
|  | 4633 | for (const auto& format: formats) { | 
|  | 4634 | if (formatsWritten < formatsMax) { | 
|  | 4635 | surroundFormats[formatsWritten++] = format; | 
|  | 4636 | } | 
|  | 4637 | } | 
|  | 4638 | return NO_ERROR; | 
|  | 4639 | } | 
|  | 4640 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4641 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) | 
|  | 4642 | { | 
| Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4643 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); | 
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 4644 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); | 
|  | 4645 | if (formatIter == mConfig.getSurroundFormats().end()) { | 
| Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4646 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4647 | return BAD_VALUE; | 
|  | 4648 | } | 
|  | 4649 |  | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4650 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != | 
|  | 4651 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { | 
| Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4652 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4653 | return INVALID_OPERATION; | 
|  | 4654 | } | 
|  | 4655 |  | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4656 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4657 | return NO_ERROR; | 
|  | 4658 | } | 
|  | 4659 |  | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4660 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4661 | if (enabled) { | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4662 | mManualSurroundFormats.insert(audioFormat); | 
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 4663 | for (const auto& subFormat : formatIter->second) { | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4664 | mManualSurroundFormats.insert(subFormat); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4665 | } | 
|  | 4666 | } else { | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4667 | mManualSurroundFormats.erase(audioFormat); | 
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 4668 | for (const auto& subFormat : formatIter->second) { | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4669 | mManualSurroundFormats.erase(subFormat); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4670 | } | 
|  | 4671 | } | 
|  | 4672 |  | 
|  | 4673 | sp<SwAudioOutputDescriptor> outputDesc; | 
|  | 4674 | bool profileUpdated = false; | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 4675 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromType( | 
|  | 4676 | AUDIO_DEVICE_OUT_HDMI); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4677 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { | 
|  | 4678 | // Simulate reconnection to update enabled surround sound formats. | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4679 | String8 address = String8(hdmiOutputDevices[i]->address().c_str()); | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4680 | std::string name = hdmiOutputDevices[i]->getName(); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4681 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, | 
|  | 4682 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 4683 | address.c_str(), | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4684 | name.c_str(), | 
|  | 4685 | AUDIO_FORMAT_DEFAULT); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4686 | if (status != NO_ERROR) { | 
|  | 4687 | continue; | 
|  | 4688 | } | 
|  | 4689 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, | 
|  | 4690 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 4691 | address.c_str(), | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4692 | name.c_str(), | 
|  | 4693 | AUDIO_FORMAT_DEFAULT); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4694 | profileUpdated |= (status == NO_ERROR); | 
|  | 4695 | } | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4696 | // 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] | 4697 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromType( | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4698 | AUDIO_DEVICE_IN_HDMI); | 
|  | 4699 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { | 
|  | 4700 | // Simulate reconnection to update enabled surround sound formats. | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4701 | String8 address = String8(hdmiInputDevices[i]->address().c_str()); | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 4702 | std::string name = hdmiInputDevices[i]->getName(); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4703 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, | 
|  | 4704 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 4705 | address.c_str(), | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4706 | name.c_str(), | 
|  | 4707 | AUDIO_FORMAT_DEFAULT); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4708 | if (status != NO_ERROR) { | 
|  | 4709 | continue; | 
|  | 4710 | } | 
|  | 4711 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, | 
|  | 4712 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 4713 | address.c_str(), | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4714 | name.c_str(), | 
|  | 4715 | AUDIO_FORMAT_DEFAULT); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4716 | profileUpdated |= (status == NO_ERROR); | 
|  | 4717 | } | 
|  | 4718 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4719 | if (!profileUpdated) { | 
| Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 4720 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4721 | mManualSurroundFormats = std::move(surroundFormatsBackup); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4722 | } | 
|  | 4723 |  | 
|  | 4724 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; | 
|  | 4725 | } | 
|  | 4726 |  | 
| Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 4727 | void AudioPolicyManager::setAppState(audio_port_handle_t portId, app_state_t state) | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 4728 | { | 
| Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 4729 | ALOGV("%s(portId:%d, state:%d)", __func__, portId, state); | 
| Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 4730 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 5ada82e | 2019-08-29 17:53:54 -0700 | [diff] [blame] | 4731 | mInputs.valueAt(i)->setAppState(portId, state); | 
| Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 4732 | } | 
|  | 4733 | } | 
|  | 4734 |  | 
| jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 4735 | bool AudioPolicyManager::isHapticPlaybackSupported() | 
|  | 4736 | { | 
|  | 4737 | for (const auto& hwModule : mHwModules) { | 
|  | 4738 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); | 
|  | 4739 | for (const auto &outProfile : outputProfiles) { | 
|  | 4740 | struct audio_port audioPort; | 
|  | 4741 | outProfile->toAudioPort(&audioPort); | 
|  | 4742 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { | 
|  | 4743 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { | 
|  | 4744 | return true; | 
|  | 4745 | } | 
|  | 4746 | } | 
|  | 4747 | } | 
|  | 4748 | } | 
|  | 4749 | return false; | 
|  | 4750 | } | 
|  | 4751 |  | 
| Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 4752 | bool AudioPolicyManager::isCallScreenModeSupported() | 
|  | 4753 | { | 
|  | 4754 | return getConfig().isCallScreenModeSupported(); | 
|  | 4755 | } | 
|  | 4756 |  | 
|  | 4757 |  | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4758 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4759 | { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4760 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4761 | if (!sourceDesc->isConnected()) { | 
|  | 4762 | ALOGV("%s port Id %d already disconnected", __FUNCTION__, sourceDesc->portId()); | 
|  | 4763 | return NO_ERROR; | 
|  | 4764 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4765 | sp<SwAudioOutputDescriptor> swOutput = sourceDesc->swOutput().promote(); | 
|  | 4766 | if (swOutput != 0) { | 
|  | 4767 | status_t status = stopSource(swOutput, sourceDesc); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4768 | if (status == NO_ERROR) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 4769 | swOutput->stop(); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4770 | } | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 4771 | if (releaseOutput(sourceDesc->portId())) { | 
|  | 4772 | // The output descriptor is reopened to query dynamic profiles. In that case, there is | 
|  | 4773 | // no need to release audio patch here but just return NO_ERROR. | 
|  | 4774 | return NO_ERROR; | 
|  | 4775 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4776 | } else { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4777 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4778 | if (hwOutputDesc != 0) { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4779 | //   close Hwoutput and remove from mHwOutputs | 
|  | 4780 | } else { | 
|  | 4781 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); | 
|  | 4782 | } | 
|  | 4783 | } | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 4784 | status_t status = releaseAudioPatchInternal(sourceDesc->getPatchHandle()); | 
|  | 4785 | sourceDesc->disconnect(); | 
|  | 4786 | return status; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4787 | } | 
|  | 4788 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4789 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForAttributesOnOutput( | 
|  | 4790 | audio_io_handle_t output, const audio_attributes_t &attr) | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4791 | { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4792 | sp<SourceClientDescriptor> source; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4793 | for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4794 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4795 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 4796 | if (followsSameRouting(attr, sourceDesc->attributes()) && | 
|  | 4797 | outputDesc != 0 && outputDesc->mIoHandle == output) { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4798 | source = sourceDesc; | 
|  | 4799 | break; | 
|  | 4800 | } | 
|  | 4801 | } | 
|  | 4802 | return source; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 4803 | } | 
|  | 4804 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4805 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4806 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4807 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4808 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
|  | 4809 | { | 
| Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 4810 | return mAudioPortGeneration++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4811 | } | 
|  | 4812 |  | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 4813 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { | 
| Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 4814 | if (std::string audioPolicyXmlConfigFile = audio_get_audio_policy_config_file(); | 
|  | 4815 | !audioPolicyXmlConfigFile.empty()) { | 
|  | 4816 | status_t ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile.c_str(), &config); | 
|  | 4817 | if (ret == NO_ERROR) { | 
|  | 4818 | config.setSource(audioPolicyXmlConfigFile); | 
| Cheney Ni | 6851adb | 2018-11-01 06:30:37 +0800 | [diff] [blame] | 4819 | } | 
| Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 4820 | return ret; | 
| Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 4821 | } | 
| Mikhail Naganov | 946c003 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 4822 | return BAD_VALUE; | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 4823 | } | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 4824 |  | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4825 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, | 
|  | 4826 | bool /*forTesting*/) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4827 | : | 
| Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 4828 | 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] | 4829 | mpClientInterface(clientInterface), | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4830 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4831 | mA2dpSuspended(false), | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4832 | mConfig(mHwModulesAll, mOutputDevicesAll, mInputDevicesAll, mDefaultOutputDevice), | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4833 | mAudioPortGeneration(1), | 
|  | 4834 | mBeaconMuteRefCount(0), | 
|  | 4835 | mBeaconPlayingRefCount(0), | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4836 | mBeaconMuted(false), | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4837 | mTtsOutputAvailable(false), | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4838 | mMasterMono(false), | 
| Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 4839 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4840 | { | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4841 | } | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4842 |  | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4843 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
|  | 4844 | : AudioPolicyManager(clientInterface, false /*forTesting*/) | 
|  | 4845 | { | 
|  | 4846 | loadConfig(); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4847 | } | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4848 |  | 
| Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 4849 | void AudioPolicyManager::loadConfig() { | 
|  | 4850 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4851 | ALOGE("could not load audio policy configuration file, setting defaults"); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4852 | getConfig().setDefault(); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4853 | } | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4854 | } | 
|  | 4855 |  | 
|  | 4856 | status_t AudioPolicyManager::initialize() { | 
| Mikhail Naganov | 4783555 | 2019-05-14 10:32:51 -0700 | [diff] [blame] | 4857 | { | 
|  | 4858 | auto engLib = EngineLibrary::load( | 
|  | 4859 | "libaudiopolicyengine" + getConfig().getEngineLibraryNameSuffix() + ".so"); | 
|  | 4860 | if (!engLib) { | 
|  | 4861 | ALOGE("%s: Failed to load the engine library", __FUNCTION__); | 
|  | 4862 | return NO_INIT; | 
|  | 4863 | } | 
|  | 4864 | mEngine = engLib->createEngine(); | 
|  | 4865 | if (mEngine == nullptr) { | 
|  | 4866 | ALOGE("%s: Failed to instantiate the APM engine", __FUNCTION__); | 
|  | 4867 | return NO_INIT; | 
|  | 4868 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4869 | } | 
|  | 4870 | mEngine->setObserver(this); | 
|  | 4871 | status_t status = mEngine->initCheck(); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4872 | if (status != NO_ERROR) { | 
|  | 4873 | LOG_FATAL("Policy engine not initialized(err=%d)", status); | 
|  | 4874 | return status; | 
|  | 4875 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4876 |  | 
| Eric Laurent | 1d69c87 | 2021-01-11 18:53:01 +0100 | [diff] [blame] | 4877 | mCommunnicationStrategy = mEngine->getProductStrategyForAttributes( | 
|  | 4878 | mEngine->getAttributesForStreamType(AUDIO_STREAM_VOICE_CALL)); | 
|  | 4879 |  | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4880 | // after parsing the config, mOutputDevicesAll and mInputDevicesAll contain all known devices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4881 | // open all output streams needed to access attached devices | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 4882 | onNewAudioModulesAvailableInt(nullptr /*newDevices*/); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4883 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4884 | // make sure default device is reachable | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4885 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { | 
|  | 4886 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", | 
|  | 4887 | mDefaultOutputDevice->toString().c_str()); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4888 | status = NO_INIT; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4889 | } | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4890 | // If microphones address is empty, set it according to device type | 
| Eric Laurent | 736a102 | 2019-03-27 18:28:46 -0700 | [diff] [blame] | 4891 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4892 | if (mAvailableInputDevices[i]->address().empty()) { | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4893 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4894 | mAvailableInputDevices[i]->setAddress(AUDIO_BOTTOM_MICROPHONE_ADDRESS); | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4895 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 4896 | mAvailableInputDevices[i]->setAddress(AUDIO_BACK_MICROPHONE_ADDRESS); | 
| jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4897 | } | 
|  | 4898 | } | 
|  | 4899 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4900 |  | 
| Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 4901 | 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] | 4902 |  | 
| Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4903 | // Silence ALOGV statements | 
|  | 4904 | property_set("log.tag." LOG_TAG, "D"); | 
|  | 4905 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4906 | updateDevicesAndOutputs(); | 
| Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4907 | return status; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4908 | } | 
|  | 4909 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4910 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4911 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4912 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4913 | mOutputs.valueAt(i)->close(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4914 | } | 
|  | 4915 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4916 | mInputs.valueAt(i)->close(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4917 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4918 | mAvailableOutputDevices.clear(); | 
|  | 4919 | mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4920 | mOutputs.clear(); | 
|  | 4921 | mInputs.clear(); | 
|  | 4922 | mHwModules.clear(); | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4923 | mHwModulesAll.clear(); | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4924 | mManualSurroundFormats.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4925 | } | 
|  | 4926 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4927 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4928 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4929 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4930 | } | 
|  | 4931 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4932 | // --- | 
|  | 4933 |  | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4934 | void AudioPolicyManager::onNewAudioModulesAvailable() | 
|  | 4935 | { | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 4936 | DeviceVector newDevices; | 
|  | 4937 | onNewAudioModulesAvailableInt(&newDevices); | 
|  | 4938 | if (!newDevices.empty()) { | 
|  | 4939 | nextAudioPortGeneration(); | 
|  | 4940 | mpClientInterface->onAudioPortListUpdate(); | 
|  | 4941 | } | 
|  | 4942 | } | 
|  | 4943 |  | 
|  | 4944 | void AudioPolicyManager::onNewAudioModulesAvailableInt(DeviceVector *newDevices) | 
|  | 4945 | { | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4946 | for (const auto& hwModule : mHwModulesAll) { | 
|  | 4947 | if (std::find(mHwModules.begin(), mHwModules.end(), hwModule) != mHwModules.end()) { | 
|  | 4948 | continue; | 
|  | 4949 | } | 
|  | 4950 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); | 
|  | 4951 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { | 
|  | 4952 | ALOGW("could not open HW module %s", hwModule->getName()); | 
|  | 4953 | continue; | 
|  | 4954 | } | 
|  | 4955 | mHwModules.push_back(hwModule); | 
|  | 4956 | // open all output streams needed to access attached devices | 
|  | 4957 | // except for direct output streams that are only opened when they are actually | 
|  | 4958 | // required by an app. | 
|  | 4959 | // This also validates mAvailableOutputDevices list | 
|  | 4960 | for (const auto& outProfile : hwModule->getOutputProfiles()) { | 
|  | 4961 | if (!outProfile->canOpenNewIo()) { | 
|  | 4962 | ALOGE("Invalid Output profile max open count %u for profile %s", | 
|  | 4963 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); | 
|  | 4964 | continue; | 
|  | 4965 | } | 
|  | 4966 | if (!outProfile->hasSupportedDevices()) { | 
|  | 4967 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); | 
|  | 4968 | continue; | 
|  | 4969 | } | 
|  | 4970 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { | 
|  | 4971 | mTtsOutputAvailable = true; | 
|  | 4972 | } | 
|  | 4973 |  | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 4974 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); | 
|  | 4975 | DeviceVector availProfileDevices = supportedDevices.filter(mOutputDevicesAll); | 
|  | 4976 | sp<DeviceDescriptor> supportedDevice = 0; | 
|  | 4977 | if (supportedDevices.contains(mDefaultOutputDevice)) { | 
|  | 4978 | supportedDevice = mDefaultOutputDevice; | 
|  | 4979 | } else { | 
|  | 4980 | // choose first device present in profile's SupportedDevices also part of | 
|  | 4981 | // mAvailableOutputDevices. | 
|  | 4982 | if (availProfileDevices.isEmpty()) { | 
|  | 4983 | continue; | 
|  | 4984 | } | 
|  | 4985 | supportedDevice = availProfileDevices.itemAt(0); | 
|  | 4986 | } | 
|  | 4987 | if (!mOutputDevicesAll.contains(supportedDevice)) { | 
|  | 4988 | continue; | 
|  | 4989 | } | 
|  | 4990 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, | 
|  | 4991 | mpClientInterface); | 
|  | 4992 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 4993 | status_t status = outputDesc->open(nullptr, DeviceVector(supportedDevice), | 
|  | 4994 | AUDIO_STREAM_DEFAULT, | 
|  | 4995 | AUDIO_OUTPUT_FLAG_NONE, &output); | 
|  | 4996 | if (status != NO_ERROR) { | 
|  | 4997 | ALOGW("Cannot open output stream for devices %s on hw module %s", | 
|  | 4998 | supportedDevice->toString().c_str(), hwModule->getName()); | 
|  | 4999 | continue; | 
|  | 5000 | } | 
|  | 5001 | for (const auto &device : availProfileDevices) { | 
|  | 5002 | // give a valid ID to an attached device once confirmed it is reachable | 
|  | 5003 | if (!device->isAttached()) { | 
|  | 5004 | device->attach(hwModule); | 
|  | 5005 | mAvailableOutputDevices.add(device); | 
| jiabin | 1c4794b | 2020-05-05 10:08:05 -0700 | [diff] [blame] | 5006 | device->setEncapsulationInfoFromHal(mpClientInterface); | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5007 | if (newDevices) newDevices->add(device); | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5008 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
|  | 5009 | } | 
|  | 5010 | } | 
| Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5011 | if (mPrimaryOutput == nullptr && | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5012 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
|  | 5013 | mPrimaryOutput = outputDesc; | 
|  | 5014 | } | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5015 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { | 
|  | 5016 | outputDesc->close(); | 
|  | 5017 | } else { | 
|  | 5018 | addOutput(output, outputDesc); | 
|  | 5019 | setOutputDevices(outputDesc, | 
|  | 5020 | DeviceVector(supportedDevice), | 
|  | 5021 | true, | 
|  | 5022 | 0, | 
|  | 5023 | NULL); | 
|  | 5024 | } | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5025 | } | 
|  | 5026 | // open input streams needed to access attached devices to validate | 
|  | 5027 | // mAvailableInputDevices list | 
|  | 5028 | for (const auto& inProfile : hwModule->getInputProfiles()) { | 
|  | 5029 | if (!inProfile->canOpenNewIo()) { | 
|  | 5030 | ALOGE("Invalid Input profile max open count %u for profile %s", | 
|  | 5031 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); | 
|  | 5032 | continue; | 
|  | 5033 | } | 
|  | 5034 | if (!inProfile->hasSupportedDevices()) { | 
|  | 5035 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); | 
|  | 5036 | continue; | 
|  | 5037 | } | 
|  | 5038 | // chose first device present in profile's SupportedDevices also part of | 
|  | 5039 | // available input devices | 
|  | 5040 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); | 
|  | 5041 | DeviceVector availProfileDevices = supportedDevices.filter(mInputDevicesAll); | 
|  | 5042 | if (availProfileDevices.isEmpty()) { | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 5043 | ALOGV("%s: Input device list is empty! for profile %s", | 
|  | 5044 | __func__, inProfile->getTagName().c_str()); | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5045 | continue; | 
|  | 5046 | } | 
|  | 5047 | sp<AudioInputDescriptor> inputDesc = | 
|  | 5048 | new AudioInputDescriptor(inProfile, mpClientInterface); | 
|  | 5049 |  | 
|  | 5050 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
|  | 5051 | status_t status = inputDesc->open(nullptr, | 
|  | 5052 | availProfileDevices.itemAt(0), | 
|  | 5053 | AUDIO_SOURCE_MIC, | 
|  | 5054 | AUDIO_INPUT_FLAG_NONE, | 
|  | 5055 | &input); | 
|  | 5056 | if (status != NO_ERROR) { | 
|  | 5057 | ALOGW("Cannot open input stream for device %s on hw module %s", | 
|  | 5058 | availProfileDevices.toString().c_str(), | 
|  | 5059 | hwModule->getName()); | 
|  | 5060 | continue; | 
|  | 5061 | } | 
|  | 5062 | for (const auto &device : availProfileDevices) { | 
|  | 5063 | // give a valid ID to an attached device once confirmed it is reachable | 
|  | 5064 | if (!device->isAttached()) { | 
|  | 5065 | device->attach(hwModule); | 
|  | 5066 | device->importAudioPortAndPickAudioProfile(inProfile, true); | 
|  | 5067 | mAvailableInputDevices.add(device); | 
| Mikhail Naganov | d0e2c74 | 2020-03-25 15:59:59 -0700 | [diff] [blame] | 5068 | if (newDevices) newDevices->add(device); | 
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 5069 | setEngineDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
|  | 5070 | } | 
|  | 5071 | } | 
|  | 5072 | inputDesc->close(); | 
|  | 5073 | } | 
|  | 5074 | } | 
|  | 5075 | } | 
|  | 5076 |  | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5077 | void AudioPolicyManager::addOutput(audio_io_handle_t output, | 
|  | 5078 | const sp<SwAudioOutputDescriptor>& outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5079 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5080 | mOutputs.add(output, outputDesc); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5081 | applyStreamVolumes(outputDesc, DeviceTypeSet(), 0 /* delayMs */, true /* force */); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 5082 | updateMono(output); // update mono status when adding to output list | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5083 | selectOutputForMusicEffects(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5084 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5085 | } | 
|  | 5086 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5087 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) | 
|  | 5088 | { | 
| Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 5089 | if (mPrimaryOutput != 0 && mPrimaryOutput == mOutputs.valueFor(output)) { | 
|  | 5090 | ALOGV("%s: removing primary output", __func__); | 
|  | 5091 | mPrimaryOutput = nullptr; | 
|  | 5092 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5093 | mOutputs.removeItem(output); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5094 | selectOutputForMusicEffects(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5095 | } | 
|  | 5096 |  | 
| Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 5097 | void AudioPolicyManager::addInput(audio_io_handle_t input, | 
|  | 5098 | const sp<AudioInputDescriptor>& inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5099 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5100 | mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5101 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5102 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5103 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5104 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5105 | audio_policy_dev_state_t state, | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5106 | SortedVector<audio_io_handle_t>& outputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5107 | { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5108 | audio_devices_t deviceType = device->type(); | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5109 | const String8 &address = String8(device->address().c_str()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5110 | sp<SwAudioOutputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5111 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5112 | if (audio_device_is_digital(deviceType)) { | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5113 | // erase all current sample rates, formats and channel masks | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5114 | device->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5115 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5116 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5117 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 5118 | // first call getAudioPort to get the supported attributes from the HAL | 
|  | 5119 | struct audio_port_v7 port = {}; | 
|  | 5120 | device->toAudioPort(&port); | 
|  | 5121 | status_t status = mpClientInterface->getAudioPort(&port); | 
|  | 5122 | if (status == NO_ERROR) { | 
|  | 5123 | device->importAudioPort(port); | 
|  | 5124 | } | 
|  | 5125 |  | 
|  | 5126 | // then list already open outputs that can be routed to this device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5127 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 5128 | desc = mOutputs.valueAt(i); | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5129 | if (!desc->isDuplicated() && desc->supportsDevice(device) | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5130 | && desc->devicesSupportEncodedFormats({deviceType})) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5131 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", | 
|  | 5132 | mOutputs.keyAt(i), device->toString().c_str()); | 
|  | 5133 | outputs.add(mOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5134 | } | 
|  | 5135 | } | 
|  | 5136 | // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5137 | SortedVector< sp<IOProfile> > profiles; | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5138 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5139 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { | 
|  | 5140 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5141 | if (profile->supportsDevice(device)) { | 
|  | 5142 | profiles.add(profile); | 
|  | 5143 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", | 
|  | 5144 | j, hwModule->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5145 | } | 
|  | 5146 | } | 
|  | 5147 | } | 
|  | 5148 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 5149 | ALOGV("  found %zu profiles, %zu outputs", profiles.size(), outputs.size()); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5150 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5151 | if (profiles.isEmpty() && outputs.isEmpty()) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5152 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5153 | return BAD_VALUE; | 
|  | 5154 | } | 
|  | 5155 |  | 
|  | 5156 | // open outputs for matching profiles if needed. Direct outputs are also opened to | 
|  | 5157 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 5158 | 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] | 5159 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5160 |  | 
|  | 5161 | // nothing to do if one output is already opened for this profile | 
|  | 5162 | size_t j; | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5163 | for (j = 0; j < outputs.size(); j++) { | 
|  | 5164 | desc = mOutputs.valueFor(outputs.itemAt(j)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5165 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5166 | // matching profile: save the sample rates, format and channel masks supported | 
|  | 5167 | // by the profile in our device descriptor | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5168 | if (audio_device_is_digital(deviceType)) { | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5169 | device->importAudioPortAndPickAudioProfile(profile); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5170 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5171 | break; | 
|  | 5172 | } | 
|  | 5173 | } | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5174 | if (j != outputs.size()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5175 | continue; | 
|  | 5176 | } | 
|  | 5177 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5178 | if (!profile->canOpenNewIo()) { | 
|  | 5179 | ALOGW("Max Output number %u already opened for this profile %s", | 
|  | 5180 | profile->maxOpenCount, profile->getTagName().c_str()); | 
|  | 5181 | continue; | 
|  | 5182 | } | 
|  | 5183 |  | 
| Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 5184 | ALOGV("opening output for device %08x with params %s profile %p name %s", | 
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 5185 | deviceType, address.string(), profile.get(), profile->getName().c_str()); | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5186 | desc = openOutputWithProfileAndDevice(profile, DeviceVector(device)); | 
|  | 5187 | 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] | 5188 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5189 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5190 | profiles.removeAt(profile_index); | 
|  | 5191 | profile_index--; | 
|  | 5192 | } else { | 
|  | 5193 | outputs.add(output); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5194 | // Load digital format info only for digital devices | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5195 | if (audio_device_is_digital(deviceType)) { | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5196 | // TODO: when getAudioPort is ready, it may not be needed to import the audio | 
|  | 5197 | // port but just pick audio profile | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5198 | device->importAudioPortAndPickAudioProfile(profile); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5199 | } | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 5200 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5201 | if (device_distinguishes_on_address(deviceType)) { | 
|  | 5202 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", | 
|  | 5203 | device->toString().c_str()); | 
|  | 5204 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, | 
|  | 5205 | NULL/*patch handle*/); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5206 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5207 | ALOGV("checkOutputsForDevice(): adding output %d", output); | 
|  | 5208 | } | 
|  | 5209 | } | 
|  | 5210 |  | 
|  | 5211 | if (profiles.isEmpty()) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5212 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5213 | return BAD_VALUE; | 
|  | 5214 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5215 | } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5216 | // check if one opened output is not needed any more after disconnecting one device | 
|  | 5217 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 5218 | desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5219 | if (!desc->isDuplicated()) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5220 | // exact match on device | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5221 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5222 | && desc->containsSingleDeviceSupportingEncodedFormats(device)) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5223 | outputs.add(mOutputs.keyAt(i)); | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5224 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5225 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", | 
|  | 5226 | mOutputs.keyAt(i)); | 
|  | 5227 | outputs.add(mOutputs.keyAt(i)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5228 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5229 | } | 
|  | 5230 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5231 | // Clear any profiles associated with the disconnected device. | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5232 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5233 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { | 
|  | 5234 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 5235 | if (!profile->supportsDevice(device)) { | 
|  | 5236 | continue; | 
|  | 5237 | } | 
|  | 5238 | ALOGV("checkOutputsForDevice(): " | 
|  | 5239 | "clearing direct output profile %zu on module %s", | 
|  | 5240 | j, hwModule->getName()); | 
|  | 5241 | profile->clearAudioProfiles(); | 
|  | 5242 | if (!profile->hasDynamicAudioProfile()) { | 
|  | 5243 | continue; | 
|  | 5244 | } | 
|  | 5245 | // When a device is disconnected, if there is an IOProfile that contains dynamic | 
|  | 5246 | // profiles and supports the disconnected device, call getAudioPort to repopulate | 
|  | 5247 | // the capabilities of the devices that is supported by the IOProfile. | 
|  | 5248 | for (const auto& supportedDevice : profile->getSupportedDevices()) { | 
|  | 5249 | if (supportedDevice == device || | 
|  | 5250 | !mAvailableOutputDevices.contains(supportedDevice)) { | 
|  | 5251 | continue; | 
|  | 5252 | } | 
|  | 5253 | struct audio_port_v7 port; | 
|  | 5254 | supportedDevice->toAudioPort(&port); | 
|  | 5255 | status_t status = mpClientInterface->getAudioPort(&port); | 
|  | 5256 | if (status == NO_ERROR) { | 
|  | 5257 | supportedDevice->importAudioPort(port); | 
|  | 5258 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5259 | } | 
|  | 5260 | } | 
|  | 5261 | } | 
|  | 5262 | } | 
|  | 5263 | return NO_ERROR; | 
|  | 5264 | } | 
|  | 5265 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5266 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5267 | audio_policy_dev_state_t state) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5268 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5269 | sp<AudioInputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5270 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5271 | if (audio_device_is_digital(device->type())) { | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5272 | // erase all current sample rates, formats and channel masks | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5273 | device->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 5274 | } | 
|  | 5275 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5276 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5277 | // look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5278 | SortedVector< sp<IOProfile> > profiles; | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5279 | for (const auto& hwModule : mHwModules) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5280 | for (size_t profile_index = 0; | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5281 | profile_index < hwModule->getInputProfiles().size(); | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5282 | profile_index++) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5283 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5284 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5285 | if (profile->supportsDevice(device)) { | 
|  | 5286 | profiles.add(profile); | 
|  | 5287 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", | 
|  | 5288 | profile_index, hwModule->getName()); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5289 | } | 
|  | 5290 | } | 
|  | 5291 | } | 
|  | 5292 |  | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5293 | if (profiles.isEmpty()) { | 
|  | 5294 | ALOGW("%s: No input profile available for device %s", | 
|  | 5295 | __func__, device->toString().c_str()); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5296 | return BAD_VALUE; | 
|  | 5297 | } | 
|  | 5298 |  | 
|  | 5299 | // open inputs for matching profiles if needed. Direct inputs are also opened to | 
|  | 5300 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 5301 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
|  | 5302 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5303 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5304 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5305 | // nothing to do if one input is already opened for this profile | 
|  | 5306 | size_t input_index; | 
|  | 5307 | for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 5308 | desc = mInputs.valueAt(input_index); | 
|  | 5309 | if (desc->mProfile == profile) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5310 | if (audio_device_is_digital(device->type())) { | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5311 | device->importAudioPortAndPickAudioProfile(profile); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5312 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5313 | break; | 
|  | 5314 | } | 
|  | 5315 | } | 
|  | 5316 | if (input_index != mInputs.size()) { | 
|  | 5317 | continue; | 
|  | 5318 | } | 
|  | 5319 |  | 
| Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 5320 | if (!profile->canOpenNewIo()) { | 
|  | 5321 | ALOGW("Max Input number %u already opened for this profile %s", | 
|  | 5322 | profile->maxOpenCount, profile->getTagName().c_str()); | 
|  | 5323 | continue; | 
|  | 5324 | } | 
|  | 5325 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5326 | desc = new AudioInputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5327 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5328 | status_t status = desc->open(nullptr, | 
|  | 5329 | device, | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5330 | AUDIO_SOURCE_MIC, | 
|  | 5331 | AUDIO_INPUT_FLAG_NONE, | 
|  | 5332 | &input); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5333 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5334 | if (status == NO_ERROR) { | 
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 5335 | const String8& address = String8(device->address().c_str()); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5336 | if (!address.isEmpty()) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5337 | char *param = audio_device_address_to_parameter(device->type(), address); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5338 | mpClientInterface->setParameters(input, String8(param)); | 
|  | 5339 | free(param); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5340 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5341 | updateAudioProfiles(device, input, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5342 | if (!profile->hasValidAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5343 | ALOGW("checkInputsForDevice() direct input missing param"); | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5344 | desc->close(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5345 | input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5346 | } | 
|  | 5347 |  | 
| Eric Laurent | 0dd5185 | 2019-04-19 18:18:58 -0700 | [diff] [blame] | 5348 | if (input != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5349 | addInput(input, desc); | 
|  | 5350 | } | 
|  | 5351 | } // endif input != 0 | 
|  | 5352 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 5353 | if (input == AUDIO_IO_HANDLE_NONE) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5354 | ALOGW("%s could not open input for device %s", __func__, | 
|  | 5355 | device->toString().c_str()); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5356 | profiles.removeAt(profile_index); | 
|  | 5357 | profile_index--; | 
|  | 5358 | } else { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5359 | if (audio_device_is_digital(device->type())) { | 
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 5360 | device->importAudioPortAndPickAudioProfile(profile); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 5361 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5362 | ALOGV("checkInputsForDevice(): adding input %d", input); | 
|  | 5363 | } | 
|  | 5364 | } // end scan profiles | 
|  | 5365 |  | 
|  | 5366 | if (profiles.isEmpty()) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5367 | 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] | 5368 | return BAD_VALUE; | 
|  | 5369 | } | 
|  | 5370 | } else { | 
|  | 5371 | // Disconnect | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5372 | // Clear any profiles associated with the disconnected device. | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 5373 | for (const auto& hwModule : mHwModules) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5374 | for (size_t profile_index = 0; | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5375 | profile_index < hwModule->getInputProfiles().size(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5376 | profile_index++) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5377 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5378 | if (profile->supportsDevice(device)) { | 
| Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 5379 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", | 
|  | 5380 | profile_index, hwModule->getName()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5381 | profile->clearAudioProfiles(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5382 | } | 
|  | 5383 | } | 
|  | 5384 | } | 
|  | 5385 | } // end disconnect | 
|  | 5386 |  | 
|  | 5387 | return NO_ERROR; | 
|  | 5388 | } | 
|  | 5389 |  | 
|  | 5390 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5391 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5392 | { | 
|  | 5393 | ALOGV("closeOutput(%d)", output); | 
|  | 5394 |  | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5395 | sp<SwAudioOutputDescriptor> closingOutput = mOutputs.valueFor(output); | 
|  | 5396 | if (closingOutput == NULL) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5397 | ALOGW("closeOutput() unknown output %d", output); | 
|  | 5398 | return; | 
|  | 5399 | } | 
| Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 5400 | const bool closingOutputWasActive = closingOutput->isActive(); | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5401 | mPolicyMixes.closeOutput(closingOutput); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5402 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5403 | // look for duplicated outputs connected to the output being removed. | 
|  | 5404 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5405 | sp<SwAudioOutputDescriptor> dupOutput = mOutputs.valueAt(i); | 
|  | 5406 | if (dupOutput->isDuplicated() && | 
|  | 5407 | (dupOutput->mOutput1 == closingOutput || dupOutput->mOutput2 == closingOutput)) { | 
|  | 5408 | sp<SwAudioOutputDescriptor> remainingOutput = | 
|  | 5409 | dupOutput->mOutput1 == closingOutput ? dupOutput->mOutput2 : dupOutput->mOutput1; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5410 | // As all active tracks on duplicated output will be deleted, | 
|  | 5411 | // and as they were also referenced on the other output, the reference | 
|  | 5412 | // count for their stream type must be adjusted accordingly on | 
|  | 5413 | // the other output. | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5414 | const bool wasActive = remainingOutput->isActive(); | 
|  | 5415 | // Note: no-op on the closing output where all clients has already been set inactive | 
|  | 5416 | dupOutput->setAllClientsInactive(); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5417 | // stop() will be a no op if the output is still active but is needed in case all | 
|  | 5418 | // active streams refcounts where cleared above | 
|  | 5419 | if (wasActive) { | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5420 | remainingOutput->stop(); | 
| Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 5421 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5422 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
|  | 5423 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
|  | 5424 |  | 
|  | 5425 | mpClientInterface->closeOutput(duplicatedOutput); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5426 | removeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5427 | } | 
|  | 5428 | } | 
|  | 5429 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5430 | nextAudioPortGeneration(); | 
|  | 5431 |  | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5432 | ssize_t index = mAudioPatches.indexOfKey(closingOutput->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5433 | if (index >= 0) { | 
|  | 5434 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5435 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( | 
|  | 5436 | patchDesc->getAfHandle(), 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5437 | mAudioPatches.removeItemsAt(index); | 
|  | 5438 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 5439 | } | 
|  | 5440 |  | 
| Mikhail Naganov | 32ebca3 | 2019-03-22 15:42:52 -0700 | [diff] [blame] | 5441 | if (closingOutputWasActive) { | 
|  | 5442 | closingOutput->stop(); | 
|  | 5443 | } | 
| François Gaffie | 1c87855 | 2018-11-22 16:53:21 +0100 | [diff] [blame] | 5444 | closingOutput->close(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5445 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5446 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5447 | mPreviousOutputs = mOutputs; | 
| Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 5448 |  | 
|  | 5449 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if | 
|  | 5450 | // no direct outputs are open. | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5451 | if (!getMsdAudioOutDevices().isEmpty()) { | 
| Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 5452 | bool directOutputOpen = false; | 
|  | 5453 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 5454 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
|  | 5455 | directOutputOpen = true; | 
|  | 5456 | break; | 
|  | 5457 | } | 
|  | 5458 | } | 
|  | 5459 | if (!directOutputOpen) { | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 5460 | ALOGV("no direct outputs open, reset MSD patches"); | 
|  | 5461 | // TODO: The MSD patches to be established here may differ to current MSD patches due to | 
|  | 5462 | // how output devices for patching are resolved. Avoid by caching and reusing the | 
|  | 5463 | // arguments to mEngine->getOutputDevicesForAttributes() when resolving which output | 
|  | 5464 | // devices to patch to. This may be complicated by the fact that devices may become | 
|  | 5465 | // unavailable. | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5466 | setMsdOutputPatches(); | 
| Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 5467 | } | 
|  | 5468 | } | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5469 | } | 
|  | 5470 |  | 
|  | 5471 | void AudioPolicyManager::closeInput(audio_io_handle_t input) | 
|  | 5472 | { | 
|  | 5473 | ALOGV("closeInput(%d)", input); | 
|  | 5474 |  | 
|  | 5475 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
|  | 5476 | if (inputDesc == NULL) { | 
|  | 5477 | ALOGW("closeInput() unknown input %d", input); | 
|  | 5478 | return; | 
|  | 5479 | } | 
|  | 5480 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5481 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5482 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5483 | sp<DeviceDescriptor> device = inputDesc->getDevice(); | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5484 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5485 | if (index >= 0) { | 
|  | 5486 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5487 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch( | 
|  | 5488 | patchDesc->getAfHandle(), 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5489 | mAudioPatches.removeItemsAt(index); | 
|  | 5490 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 5491 | } | 
|  | 5492 |  | 
| Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 5493 | inputDesc->close(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 5494 | mInputs.removeItem(input); | 
| Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 5495 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5496 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); | 
|  | 5497 | if (primaryInputDevices.contains(device) && | 
| Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 5498 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { | 
| Ytai Ben-Tsvi | 74cd6b0 | 2019-10-25 10:06:40 -0700 | [diff] [blame] | 5499 | mpClientInterface->setSoundTriggerCaptureState(false); | 
| Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 5500 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5501 | } | 
|  | 5502 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5503 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( | 
|  | 5504 | const DeviceVector &devices, | 
|  | 5505 | const SwAudioOutputCollection& openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5506 | { | 
|  | 5507 | SortedVector<audio_io_handle_t> outputs; | 
|  | 5508 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5509 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5510 | for (size_t i = 0; i < openOutputs.size(); i++) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5511 | ALOGVV("output %zu isDuplicated=%d device=%s", | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5512 | i, openOutputs.valueAt(i)->isDuplicated(), | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5513 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5514 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5515 | && openOutputs.valueAt(i)->devicesSupportEncodedFormats(devices.types())) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5516 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5517 | outputs.add(openOutputs.keyAt(i)); | 
|  | 5518 | } | 
|  | 5519 | } | 
|  | 5520 | return outputs; | 
|  | 5521 | } | 
|  | 5522 |  | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 5523 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) | 
|  | 5524 | { | 
|  | 5525 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
|  | 5526 | // output is suspended before any tracks are moved to it | 
|  | 5527 | checkA2dpSuspend(); | 
|  | 5528 | checkOutputForAllStrategies(); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5529 | checkSecondaryOutputs(); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 5530 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 5531 | updateDevicesAndOutputs(); | 
| Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 5532 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { | 
| Michael Chan | 6fb3449 | 2020-12-08 15:44:49 +1100 | [diff] [blame] | 5533 | // TODO: The MSD patches to be established here may differ to current MSD patches due to how | 
|  | 5534 | // output devices for patching are resolved. Nevertheless, AudioTracks affected by device | 
|  | 5535 | // configuration changes will ultimately be rerouted correctly. We can still avoid | 
|  | 5536 | // unnecessary rerouting by caching and reusing the arguments to | 
|  | 5537 | // mEngine->getOutputDevicesForAttributes() when resolving which output devices to patch to. | 
|  | 5538 | // This may be complicated by the fact that devices may become unavailable. | 
| Dean Wheatley | 8bee85a | 2021-02-10 16:02:23 +1100 | [diff] [blame] | 5539 | setMsdOutputPatches(); | 
| Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 5540 | } | 
| Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 5541 | // an event that changed routing likely occurred, inform upper layers | 
|  | 5542 | mpClientInterface->onRoutingUpdated(); | 
| Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 5543 | } | 
|  | 5544 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5545 | bool AudioPolicyManager::followsSameRouting(const audio_attributes_t &lAttr, | 
|  | 5546 | const audio_attributes_t &rAttr) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5547 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5548 | return mEngine->getProductStrategyForAttributes(lAttr) == | 
|  | 5549 | mEngine->getProductStrategyForAttributes(rAttr); | 
|  | 5550 | } | 
|  | 5551 |  | 
| Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5552 | void AudioPolicyManager::checkAudioSourceForAttributes(const audio_attributes_t &attr) | 
|  | 5553 | { | 
|  | 5554 | for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
|  | 5555 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 5556 | if (sourceDesc != nullptr && followsSameRouting(attr, sourceDesc->attributes()) | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5557 | && sourceDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE | 
|  | 5558 | && !isCallRxAudioSource(sourceDesc)) { | 
| Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5559 | connectAudioSource(sourceDesc); | 
|  | 5560 | } | 
|  | 5561 | } | 
|  | 5562 | } | 
|  | 5563 |  | 
|  | 5564 | void AudioPolicyManager::clearAudioSourcesForOutput(audio_io_handle_t output) | 
|  | 5565 | { | 
|  | 5566 | for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
|  | 5567 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 5568 | if (sourceDesc != nullptr && sourceDesc->swOutput().promote() != nullptr | 
|  | 5569 | && sourceDesc->swOutput().promote()->mIoHandle == output) { | 
|  | 5570 | disconnectAudioSource(sourceDesc); | 
|  | 5571 | } | 
|  | 5572 | } | 
|  | 5573 | } | 
|  | 5574 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5575 | void AudioPolicyManager::checkOutputForAttributes(const audio_attributes_t &attr) | 
|  | 5576 | { | 
|  | 5577 | auto psId = mEngine->getProductStrategyForAttributes(attr); | 
|  | 5578 |  | 
|  | 5579 | DeviceVector oldDevices = mEngine->getOutputDevicesForAttributes(attr, 0, true /*fromCache*/); | 
|  | 5580 | DeviceVector newDevices = mEngine->getOutputDevicesForAttributes(attr, 0, false /*fromCache*/); | 
| Jean-Michel Trivi | 3085715 | 2019-11-01 11:04:15 -0700 | [diff] [blame] | 5581 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5582 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); | 
|  | 5583 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5584 |  | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5585 | uint32_t maxLatency = 0; | 
|  | 5586 | bool invalidate = false; | 
|  | 5587 | // take into account dynamic audio policies related changes: if a client is now associated | 
|  | 5588 | // to a different policy mix than at creation time, invalidate corresponding stream | 
|  | 5589 | for (size_t i = 0; i < mPreviousOutputs.size() && !invalidate; i++) { | 
|  | 5590 | const sp<SwAudioOutputDescriptor>& desc = mPreviousOutputs.valueAt(i); | 
|  | 5591 | if (desc->isDuplicated()) { | 
|  | 5592 | continue; | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 5593 | } | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5594 | for (const sp<TrackClientDescriptor>& client : desc->getClientIterable()) { | 
|  | 5595 | if (mEngine->getProductStrategyForAttributes(client->attributes()) != psId) { | 
|  | 5596 | continue; | 
|  | 5597 | } | 
|  | 5598 | sp<AudioPolicyMix> primaryMix; | 
|  | 5599 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->uid(), | 
|  | 5600 | client->flags(), primaryMix, nullptr); | 
|  | 5601 | if (status != OK) { | 
|  | 5602 | continue; | 
|  | 5603 | } | 
| yucliu | f4de36d | 2020-09-14 14:57:56 -0700 | [diff] [blame] | 5604 | if (client->getPrimaryMix() != primaryMix || client->hasLostPrimaryMix()) { | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5605 | invalidate = true; | 
|  | 5606 | if (desc->isStrategyActive(psId)) { | 
|  | 5607 | maxLatency = desc->latency(); | 
|  | 5608 | } | 
|  | 5609 | break; | 
|  | 5610 | } | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 5611 | } | 
|  | 5612 | } | 
|  | 5613 |  | 
| Eric Laurent | c209fe4 | 2020-06-05 18:11:23 -0700 | [diff] [blame] | 5614 | if (srcOutputs != dstOutputs || invalidate) { | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5615 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing | 
|  | 5616 | // audio from invalidated tracks will be rendered when unmuting | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5617 | for (audio_io_handle_t srcOut : srcOutputs) { | 
|  | 5618 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); | 
| Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5619 | if (desc == nullptr) continue; | 
|  | 5620 |  | 
|  | 5621 | if (desc->isStrategyActive(psId) && maxLatency < desc->latency()) { | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5622 | maxLatency = desc->latency(); | 
|  | 5623 | } | 
| Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5624 |  | 
|  | 5625 | if (invalidate) continue; | 
|  | 5626 |  | 
|  | 5627 | for (auto client : desc->clientsList(false /*activeOnly*/)) { | 
| Eric Laurent | 78aade8 | 2019-09-13 18:55:08 -0700 | [diff] [blame] | 5628 | if (desc->isDuplicated() || !desc->mProfile->isDirectOutput()) { | 
| Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5629 | // a client on a non direct outputs has necessarily a linear PCM format | 
|  | 5630 | // so we can call selectOutput() safely | 
|  | 5631 | const audio_io_handle_t newOutput = selectOutput(dstOutputs, | 
|  | 5632 | client->flags(), | 
|  | 5633 | client->config().format, | 
|  | 5634 | client->config().channel_mask, | 
| jiabin | ebb6af4 | 2020-06-09 17:31:17 -0700 | [diff] [blame] | 5635 | client->config().sample_rate, | 
|  | 5636 | client->session()); | 
| Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5637 | if (newOutput != srcOut) { | 
|  | 5638 | invalidate = true; | 
|  | 5639 | break; | 
|  | 5640 | } | 
|  | 5641 | } else { | 
|  | 5642 | sp<IOProfile> profile = getProfileForOutput(newDevices, | 
|  | 5643 | client->config().sample_rate, | 
|  | 5644 | client->config().format, | 
|  | 5645 | client->config().channel_mask, | 
|  | 5646 | client->flags(), | 
|  | 5647 | true /* directOnly */); | 
|  | 5648 | if (profile != desc->mProfile) { | 
|  | 5649 | invalidate = true; | 
|  | 5650 | break; | 
|  | 5651 | } | 
|  | 5652 | } | 
|  | 5653 | } | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5654 | } | 
| Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5655 |  | 
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5656 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5657 | "%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] | 5658 | std::to_string(srcOutputs[0]).c_str(), | 
|  | 5659 | std::to_string(dstOutputs[0]).c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5660 | // mute strategy while moving tracks from one output to another | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5661 | for (audio_io_handle_t srcOut : srcOutputs) { | 
| Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 5662 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); | 
| Eric Laurent | aa02db8 | 2019-09-05 17:31:49 -0700 | [diff] [blame] | 5663 | if (desc == nullptr) continue; | 
|  | 5664 |  | 
|  | 5665 | if (desc->isStrategyActive(psId)) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5666 | setStrategyMute(psId, true, desc); | 
|  | 5667 | setStrategyMute(psId, false, desc, maxLatency * LATENCY_MUTE_FACTOR, | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5668 | newDevices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5669 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5670 | sp<SourceClientDescriptor> source = getSourceForAttributesOnOutput(srcOut, attr); | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 5671 | if (source != nullptr && !isCallRxAudioSource(source)) { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5672 | connectAudioSource(source); | 
|  | 5673 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5674 | } | 
|  | 5675 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5676 | // Move effects associated to this stream from previous output to new output | 
|  | 5677 | if (followsSameRouting(attr, attributes_initializer(AUDIO_USAGE_MEDIA))) { | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 5678 | selectOutputForMusicEffects(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5679 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5680 | // 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] | 5681 | if (invalidate) { | 
|  | 5682 | for (auto stream :  mEngine->getStreamTypesForProductStrategy(psId)) { | 
|  | 5683 | mpClientInterface->invalidateStream(stream); | 
|  | 5684 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5685 | } | 
|  | 5686 | } | 
|  | 5687 | } | 
|  | 5688 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5689 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5690 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5691 | for (const auto &strategy : mEngine->getOrderedProductStrategies()) { | 
|  | 5692 | auto attributes = mEngine->getAllAttributesForProductStrategy(strategy).front(); | 
|  | 5693 | checkOutputForAttributes(attributes); | 
| Francois Gaffie | ff1eb52 | 2020-05-06 18:37:04 +0200 | [diff] [blame] | 5694 | checkAudioSourceForAttributes(attributes); | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5695 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5696 | } | 
|  | 5697 |  | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5698 | void AudioPolicyManager::checkSecondaryOutputs() { | 
|  | 5699 | std::set<audio_stream_type_t> streamsToInvalidate; | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 5700 | TrackSecondaryOutputsMap trackSecondaryOutputs; | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5701 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 5702 | const sp<SwAudioOutputDescriptor>& outputDescriptor = mOutputs[i]; | 
|  | 5703 | for (const sp<TrackClientDescriptor>& client : outputDescriptor->getClientIterable()) { | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5704 | sp<AudioPolicyMix> primaryMix; | 
|  | 5705 | std::vector<sp<AudioPolicyMix>> secondaryMixes; | 
| Kevin Rocard | 94114a2 | 2019-04-01 19:38:23 -0700 | [diff] [blame] | 5706 | status_t status = mPolicyMixes.getOutputForAttr(client->attributes(), client->uid(), | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5707 | client->flags(), primaryMix, &secondaryMixes); | 
|  | 5708 | std::vector<sp<SwAudioOutputDescriptor>> secondaryDescs; | 
|  | 5709 | for (auto &secondaryMix : secondaryMixes) { | 
|  | 5710 | sp<SwAudioOutputDescriptor> outputDesc = secondaryMix->getOutput(); | 
|  | 5711 | if (outputDesc != nullptr && | 
|  | 5712 | outputDesc->mIoHandle != AUDIO_IO_HANDLE_NONE) { | 
|  | 5713 | secondaryDescs.push_back(outputDesc); | 
|  | 5714 | } | 
|  | 5715 | } | 
|  | 5716 |  | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 5717 | if (status != OK) { | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5718 | streamsToInvalidate.insert(client->stream()); | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 5719 | } else if (!std::equal( | 
|  | 5720 | client->getSecondaryOutputs().begin(), | 
|  | 5721 | client->getSecondaryOutputs().end(), | 
|  | 5722 | secondaryDescs.begin(), secondaryDescs.end())) { | 
|  | 5723 | std::vector<wp<SwAudioOutputDescriptor>> weakSecondaryDescs; | 
|  | 5724 | std::vector<audio_io_handle_t> secondaryOutputIds; | 
|  | 5725 | for (const auto& secondaryDesc : secondaryDescs) { | 
|  | 5726 | secondaryOutputIds.push_back(secondaryDesc->mIoHandle); | 
|  | 5727 | weakSecondaryDescs.push_back(secondaryDesc); | 
|  | 5728 | } | 
|  | 5729 | trackSecondaryOutputs.emplace(client->portId(), secondaryOutputIds); | 
|  | 5730 | client->setSecondaryOutputs(std::move(weakSecondaryDescs)); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5731 | } | 
|  | 5732 | } | 
|  | 5733 | } | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 5734 | if (!trackSecondaryOutputs.empty()) { | 
|  | 5735 | mpClientInterface->updateSecondaryOutputs(trackSecondaryOutputs); | 
|  | 5736 | } | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5737 | for (audio_stream_type_t stream : streamsToInvalidate) { | 
| jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 5738 | ALOGD("%s Invalidate stream %d due to fail getting output for attr", __func__, stream); | 
| Kevin Rocard | 153f92d | 2018-12-18 18:33:28 -0800 | [diff] [blame] | 5739 | mpClientInterface->invalidateStream(stream); | 
|  | 5740 | } | 
|  | 5741 | } | 
|  | 5742 |  | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5743 | bool AudioPolicyManager::isScoRequestedForComm() const { | 
|  | 5744 | AudioDeviceTypeAddrVector devices; | 
|  | 5745 | mEngine->getDevicesForRoleAndStrategy(mCommunnicationStrategy, DEVICE_ROLE_PREFERRED, devices); | 
|  | 5746 | for (const auto &device : devices) { | 
|  | 5747 | if (audio_is_bluetooth_out_sco_device(device.mType)) { | 
|  | 5748 | return true; | 
|  | 5749 | } | 
|  | 5750 | } | 
|  | 5751 | return false; | 
|  | 5752 | } | 
|  | 5753 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5754 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5755 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5756 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); | 
| Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 5757 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5758 | mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5759 | return; | 
|  | 5760 | } | 
|  | 5761 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5762 | bool isScoConnected = | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5763 | (mAvailableInputDevices.types().count(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) != 0 || | 
|  | 5764 | !Intersection(mAvailableOutputDevices.types(), getAudioDeviceOutAllScoSet()).empty()); | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5765 | bool isScoRequested = isScoRequestedForComm(); | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5766 |  | 
|  | 5767 | // if suspended, restore A2DP output if: | 
|  | 5768 | //      ((SCO device is NOT connected) || | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5769 | //       ((SCO is not requested) && | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5770 | //        (phone state is NOT in call) && (phone state is NOT ringing))) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5771 | // | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5772 | // if not suspended, suspend A2DP output if: | 
|  | 5773 | //      (SCO device is connected) && | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5774 | //       ((SCO is requested) || | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5775 | //       ((phone state is in call) || (phone state is ringing))) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5776 | // | 
|  | 5777 | if (mA2dpSuspended) { | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5778 | if (!isScoConnected || | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5779 | (!isScoRequested && | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5780 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5781 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5782 |  | 
|  | 5783 | mpClientInterface->restoreOutput(a2dpOutput); | 
|  | 5784 | mA2dpSuspended = false; | 
|  | 5785 | } | 
|  | 5786 | } else { | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5787 | if (isScoConnected && | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 5788 | (isScoRequested || | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 5789 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5790 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5791 |  | 
|  | 5792 | mpClientInterface->suspendOutput(a2dpOutput); | 
|  | 5793 | mA2dpSuspended = true; | 
|  | 5794 | } | 
|  | 5795 | } | 
|  | 5796 | } | 
|  | 5797 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5798 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, | 
|  | 5799 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5800 | { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5801 | DeviceVector devices; | 
|  | 5802 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5803 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5804 | if (index >= 0) { | 
|  | 5805 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5806 | if (patchDesc->getUid() != mUidCached) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5807 | ALOGV("%s device %s forced by patch %d", __func__, | 
|  | 5808 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); | 
|  | 5809 | return  outputDesc->devices(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5810 | } | 
|  | 5811 | } | 
|  | 5812 |  | 
| Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 5813 | // Do not retrieve engine device for outputs through MSD | 
|  | 5814 | // TODO: support explicit routing requests by resetting MSD patch to engine device. | 
|  | 5815 | if (outputDesc->devices() == getMsdAudioOutDevices()) { | 
|  | 5816 | return outputDesc->devices(); | 
|  | 5817 | } | 
|  | 5818 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5819 | // Honor explicit routing requests only if no client using default routing is active on this | 
|  | 5820 | // input: a specific app can not force routing for other apps by setting a preferred device. | 
|  | 5821 | bool active; // unused | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5822 | sp<DeviceDescriptor> device = | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5823 | findPreferredDevice(outputDesc, PRODUCT_STRATEGY_NONE, active, mAvailableOutputDevices); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5824 | if (device != nullptr) { | 
|  | 5825 | return DeviceVector(device); | 
| Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 5826 | } | 
|  | 5827 |  | 
| François Gaffie | a807ef9 | 2018-11-05 10:44:33 +0100 | [diff] [blame] | 5828 | // Legacy Engine cannot take care of bus devices and mix, so we need to handle the conflict | 
|  | 5829 | // of setForceUse / Default Bus device here | 
|  | 5830 | device = mPolicyMixes.getDeviceAndMixForOutput(outputDesc, mAvailableOutputDevices); | 
|  | 5831 | if (device != nullptr) { | 
|  | 5832 | return DeviceVector(device); | 
|  | 5833 | } | 
|  | 5834 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5835 | for (const auto &productStrategy : mEngine->getOrderedProductStrategies()) { | 
|  | 5836 | StreamTypeVector streams = mEngine->getStreamTypesForProductStrategy(productStrategy); | 
|  | 5837 | auto attr = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); | 
| Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 5838 | auto hasStreamActive = [&](auto stream) { | 
|  | 5839 | return hasStream(streams, stream) && isStreamActive(stream, 0); | 
|  | 5840 | }; | 
| Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5841 |  | 
| Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 5842 | auto doGetOutputDevicesForVoice = [&]() { | 
|  | 5843 | return hasVoiceStream(streams) && (outputDesc == mPrimaryOutput || | 
|  | 5844 | outputDesc->isActive(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) && | 
|  | 5845 | (isInCall() || | 
|  | 5846 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc)); | 
|  | 5847 | }; | 
|  | 5848 |  | 
|  | 5849 | // With low-latency playing on speaker, music on WFD, when the first low-latency | 
|  | 5850 | // output is stopped, getNewOutputDevices checks for a product strategy | 
|  | 5851 | // from the list, as STRATEGY_SONIFICATION comes prior to STRATEGY_MEDIA. | 
| Carter Hsu | cc58a6b | 2021-07-20 08:44:50 +0000 | [diff] [blame] | 5852 | // If an ALARM or ENFORCED_AUDIBLE stream is supported by the product strategy, | 
| Jaideep Sharma | e4d123a | 2020-11-24 15:14:03 +0530 | [diff] [blame] | 5853 | // devices are returned for STRATEGY_SONIFICATION without checking whether the | 
|  | 5854 | // stream is associated to the output descriptor. | 
|  | 5855 | if (doGetOutputDevicesForVoice() || outputDesc->isStrategyActive(productStrategy) || | 
|  | 5856 | ((hasStreamActive(AUDIO_STREAM_ALARM) || | 
|  | 5857 | hasStreamActive(AUDIO_STREAM_ENFORCED_AUDIBLE)) && | 
|  | 5858 | mOutputs.isStrategyActiveOnSameModule(productStrategy, outputDesc))) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5859 | // Retrieval of devices for voice DL is done on primary output profile, cannot | 
|  | 5860 | // check the route (would force modifying configuration file for this profile) | 
|  | 5861 | devices = mEngine->getOutputDevicesForAttributes(attr, nullptr, fromCache); | 
|  | 5862 | break; | 
|  | 5863 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5864 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5865 | ALOGV("%s selected devices %s", __func__, devices.toString().c_str()); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5866 | return devices; | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5867 | } | 
|  | 5868 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5869 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( | 
|  | 5870 | const sp<AudioInputDescriptor>& inputDesc) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5871 | { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5872 | sp<DeviceDescriptor> device; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5873 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5874 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5875 | if (index >= 0) { | 
|  | 5876 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 5877 | if (patchDesc->getUid() != mUidCached) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5878 | ALOGV("getNewInputDevice() device %s forced by patch %d", | 
|  | 5879 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); | 
|  | 5880 | return inputDesc->getDevice(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5881 | } | 
|  | 5882 | } | 
|  | 5883 |  | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5884 | // Honor explicit routing requests only if no client using default routing is active on this | 
|  | 5885 | // input: a specific app can not force routing for other apps by setting a preferred device. | 
|  | 5886 | bool active; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5887 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); | 
|  | 5888 | if (device != nullptr) { | 
|  | 5889 | return device; | 
| Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5890 | } | 
|  | 5891 |  | 
| Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5892 | // 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] | 5893 | // a null sp<>, causing the patch on the input stream to be released. | 
| yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 5894 | audio_attributes_t attributes; | 
|  | 5895 | uid_t uid; | 
|  | 5896 | sp<RecordClientDescriptor> topClient = inputDesc->getHighestPriorityClient(); | 
|  | 5897 | if (topClient != nullptr) { | 
|  | 5898 | attributes = topClient->attributes(); | 
|  | 5899 | uid = topClient->uid(); | 
|  | 5900 | } else { | 
|  | 5901 | attributes = { .source = AUDIO_SOURCE_DEFAULT }; | 
|  | 5902 | uid = 0; | 
|  | 5903 | } | 
|  | 5904 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5905 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { | 
|  | 5906 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; | 
| Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5907 | } | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5908 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { | 
| yuanjiahsu | 0735bf3 | 2021-03-18 08:12:54 +0800 | [diff] [blame] | 5909 | device = mEngine->getInputDeviceForAttributes(attributes, uid); | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5910 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5911 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5912 | return device; | 
|  | 5913 | } | 
|  | 5914 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5915 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, | 
|  | 5916 | audio_stream_type_t stream2) { | 
| Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 5917 | return (stream1 == stream2); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5918 | } | 
|  | 5919 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5920 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5921 | // By checking the range of stream before calling getStrategy, we avoid | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5922 | // getOutputDevicesForStream's behavior for invalid streams. | 
|  | 5923 | // engine's getOutputDevicesForStream would fallback on its default behavior (most probably | 
|  | 5924 | // device for music stream), but we want to return the empty set. | 
|  | 5925 | if (stream < AUDIO_STREAM_MIN || stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5926 | return AUDIO_DEVICE_NONE; | 
|  | 5927 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5928 | DeviceVector activeDevices; | 
|  | 5929 | DeviceVector devices; | 
| Mikhail Naganov | f33115d | 2020-09-25 23:03:05 +0000 | [diff] [blame] | 5930 | for (int i = AUDIO_STREAM_MIN; i < AUDIO_STREAM_PUBLIC_CNT; ++i) { | 
|  | 5931 | 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] | 5932 | if (!streamsMatchForvolume(stream, curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5933 | continue; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5934 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5935 | DeviceVector curDevices = mEngine->getOutputDevicesForStream(curStream, false/*fromCache*/); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5936 | devices.merge(curDevices); | 
|  | 5937 | for (audio_io_handle_t output : getOutputsForDevices(curDevices, mOutputs)) { | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5938 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | 83d17c2 | 2019-04-02 17:10:01 -0700 | [diff] [blame] | 5939 | if (outputDesc->isActive(toVolumeSource(curStream))) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5940 | activeDevices.merge(outputDesc->devices()); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5941 | } | 
|  | 5942 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5943 | } | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5944 |  | 
| Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 5945 | // Favor devices selected on active streams if any to report correct device in case of | 
|  | 5946 | // explicit device selection | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5947 | if (!activeDevices.isEmpty()) { | 
| Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 5948 | devices = activeDevices; | 
|  | 5949 | } | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5950 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it | 
|  | 5951 | and doesn't really need to.*/ | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5952 | DeviceVector speakerSafeDevices = devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER_SAFE); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5953 | if (!speakerSafeDevices.isEmpty()) { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5954 | devices.merge(mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER)); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5955 | devices.remove(speakerSafeDevices); | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5956 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 5957 | // FIXME: use DeviceTypeSet when Java layer is ready for it. | 
|  | 5958 | return deviceTypesToBitMask(devices.types()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5959 | } | 
|  | 5960 |  | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5961 | status_t AudioPolicyManager::getDevicesForAttributes( | 
|  | 5962 | const audio_attributes_t &attr, AudioDeviceTypeAddrVector *devices) { | 
|  | 5963 | if (devices == nullptr) { | 
|  | 5964 | return BAD_VALUE; | 
|  | 5965 | } | 
|  | 5966 | // check dynamic policies but only for primary descriptors (secondary not used for audible | 
|  | 5967 | // audio routing, only used for duplication for playback capture) | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5968 | sp<AudioPolicyMix> policyMix; | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5969 | status_t status = mPolicyMixes.getOutputForAttr(attr, 0 /*uid unknown here*/, | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5970 | AUDIO_OUTPUT_FLAG_NONE, policyMix, nullptr); | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5971 | if (status != OK) { | 
|  | 5972 | return status; | 
|  | 5973 | } | 
| Eric Laurent | c529cf6 | 2020-04-17 18:19:10 -0700 | [diff] [blame] | 5974 | if (policyMix != nullptr && policyMix->getOutput() != nullptr) { | 
|  | 5975 | AudioDeviceTypeAddr device(policyMix->mDeviceType, policyMix->mDeviceAddress.c_str()); | 
|  | 5976 | devices->push_back(device); | 
|  | 5977 | return NO_ERROR; | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5978 | } | 
| Jean-Michel Trivi | f41599b | 2020-01-07 14:22:08 -0800 | [diff] [blame] | 5979 | DeviceVector curDevices = mEngine->getOutputDevicesForAttributes(attr, nullptr, false); | 
|  | 5980 | for (const auto& device : curDevices) { | 
|  | 5981 | devices->push_back(device->getDeviceTypeAddr()); | 
|  | 5982 | } | 
|  | 5983 | return NO_ERROR; | 
|  | 5984 | } | 
|  | 5985 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5986 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5987 | switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5988 | case AUDIO_STREAM_MUSIC: | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 5989 | checkOutputForAttributes(attributes_initializer(AUDIO_USAGE_NOTIFICATION)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5990 | updateDevicesAndOutputs(); | 
|  | 5991 | break; | 
|  | 5992 | default: | 
|  | 5993 | break; | 
|  | 5994 | } | 
|  | 5995 | } | 
|  | 5996 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5997 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5998 |  | 
|  | 5999 | // skip beacon mute management if a dedicated TTS output is available | 
|  | 6000 | if (mTtsOutputAvailable) { | 
|  | 6001 | return 0; | 
|  | 6002 | } | 
|  | 6003 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6004 | switch(event) { | 
|  | 6005 | case STARTING_OUTPUT: | 
|  | 6006 | mBeaconMuteRefCount++; | 
|  | 6007 | break; | 
|  | 6008 | case STOPPING_OUTPUT: | 
|  | 6009 | if (mBeaconMuteRefCount > 0) { | 
|  | 6010 | mBeaconMuteRefCount--; | 
|  | 6011 | } | 
|  | 6012 | break; | 
|  | 6013 | case STARTING_BEACON: | 
|  | 6014 | mBeaconPlayingRefCount++; | 
|  | 6015 | break; | 
|  | 6016 | case STOPPING_BEACON: | 
|  | 6017 | if (mBeaconPlayingRefCount > 0) { | 
|  | 6018 | mBeaconPlayingRefCount--; | 
|  | 6019 | } | 
|  | 6020 | break; | 
|  | 6021 | } | 
|  | 6022 |  | 
|  | 6023 | if (mBeaconMuteRefCount > 0) { | 
|  | 6024 | // any playback causes beacon to be muted | 
|  | 6025 | return setBeaconMute(true); | 
|  | 6026 | } else { | 
|  | 6027 | // no other playback: unmute when beacon starts playing, mute when it stops | 
|  | 6028 | return setBeaconMute(mBeaconPlayingRefCount == 0); | 
|  | 6029 | } | 
|  | 6030 | } | 
|  | 6031 |  | 
|  | 6032 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { | 
|  | 6033 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", | 
|  | 6034 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); | 
|  | 6035 | // keep track of muted state to avoid repeating mute/unmute operations | 
|  | 6036 | if (mBeaconMuted != mute) { | 
|  | 6037 | // mute/unmute AUDIO_STREAM_TTS on all outputs | 
|  | 6038 | ALOGV("\t muting %d", mute); | 
|  | 6039 | uint32_t maxLatency = 0; | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6040 | auto ttsVolumeSource = toVolumeSource(AUDIO_STREAM_TTS); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6041 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6042 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6043 | setVolumeSourceMute(ttsVolumeSource, mute/*on*/, desc, 0 /*delay*/, DeviceTypeSet()); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6044 | const uint32_t latency = desc->latency() * 2; | 
| Eric Laurent | cdb2b35 | 2020-07-23 10:57:02 -0700 | [diff] [blame] | 6045 | if (desc->isActive(latency * 2) && latency > maxLatency) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 6046 | maxLatency = latency; | 
|  | 6047 | } | 
|  | 6048 | } | 
|  | 6049 | mBeaconMuted = mute; | 
|  | 6050 | return maxLatency; | 
|  | 6051 | } | 
|  | 6052 | return 0; | 
|  | 6053 | } | 
|  | 6054 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 6055 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6056 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6057 | mEngine->updateDeviceSelectionCache(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6058 | mPreviousOutputs = mOutputs; | 
|  | 6059 | } | 
|  | 6060 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 6061 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6062 | const DeviceVector &prevDevices, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6063 | uint32_t delayMs) | 
|  | 6064 | { | 
|  | 6065 | // mute/unmute strategies using an incompatible device combination | 
|  | 6066 | // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
|  | 6067 | // if unmuting, unmute only after the specified delay | 
|  | 6068 | if (outputDesc->isDuplicated()) { | 
|  | 6069 | return 0; | 
|  | 6070 | } | 
|  | 6071 |  | 
|  | 6072 | uint32_t muteWaitMs = 0; | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6073 | DeviceVector devices = outputDesc->devices(); | 
|  | 6074 | bool shouldMute = outputDesc->isActive() && (devices.size() >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6075 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6076 | auto productStrategies = mEngine->getOrderedProductStrategies(); | 
|  | 6077 | for (const auto &productStrategy : productStrategies) { | 
|  | 6078 | auto attributes = mEngine->getAllAttributesForProductStrategy(productStrategy).front(); | 
|  | 6079 | DeviceVector curDevices = | 
|  | 6080 | mEngine->getOutputDevicesForAttributes(attributes, nullptr, false/*fromCache*/); | 
|  | 6081 | curDevices = curDevices.filter(outputDesc->supportedDevices()); | 
|  | 6082 | bool mute = shouldMute && curDevices.containsAtLeastOne(devices) && curDevices != devices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6083 | bool doMute = false; | 
|  | 6084 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6085 | if (mute && !outputDesc->isStrategyMutedByDevice(productStrategy)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6086 | doMute = true; | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6087 | outputDesc->setStrategyMutedByDevice(productStrategy, true); | 
|  | 6088 | } else if (!mute && outputDesc->isStrategyMutedByDevice(productStrategy)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6089 | doMute = true; | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6090 | outputDesc->setStrategyMutedByDevice(productStrategy, false); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6091 | } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6092 | if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6093 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6094 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6095 | // 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] | 6096 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6097 | continue; | 
|  | 6098 | } | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6099 | ALOGVV("%s() %s (curDevice %s)", __func__, | 
|  | 6100 | mute ? "muting" : "unmuting", curDevices.toString().c_str()); | 
|  | 6101 | setStrategyMute(productStrategy, mute, desc, mute ? 0 : delayMs); | 
|  | 6102 | if (desc->isStrategyActive(productStrategy)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6103 | if (mute) { | 
|  | 6104 | // FIXME: should not need to double latency if volume could be applied | 
|  | 6105 | // immediately by the audioflinger mixer. We must account for the delay | 
|  | 6106 | // between now and the next time the audioflinger thread for this output | 
|  | 6107 | // will process a buffer (which corresponds to one buffer size, | 
|  | 6108 | // usually 1/2 or 1/4 of the latency). | 
|  | 6109 | if (muteWaitMs < desc->latency() * 2) { | 
|  | 6110 | muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6111 | } | 
|  | 6112 | } | 
|  | 6113 | } | 
|  | 6114 | } | 
|  | 6115 | } | 
|  | 6116 | } | 
|  | 6117 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6118 | // temporary mute output if device selection changes to avoid volume bursts due to | 
|  | 6119 | // different per device volumes | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6120 | if (outputDesc->isActive() && (devices != prevDevices)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 6121 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; | 
|  | 6122 | // temporary mute duration is conservatively set to 4 times the reported latency | 
|  | 6123 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; | 
|  | 6124 | if (muteWaitMs < tempMuteWaitMs) { | 
|  | 6125 | muteWaitMs = tempMuteWaitMs; | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6126 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6127 | for (const auto &activeVs : outputDesc->getActiveVolumeSources()) { | 
|  | 6128 | // make sure that we do not start the temporary mute period too early in case of | 
|  | 6129 | // delayed device change | 
|  | 6130 | setVolumeSourceMute(activeVs, true, outputDesc, delayMs); | 
|  | 6131 | setVolumeSourceMute(activeVs, false, outputDesc, delayMs + tempMuteDurationMs, | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6132 | devices.types()); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 6133 | } | 
|  | 6134 | } | 
|  | 6135 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6136 | // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
|  | 6137 | if (muteWaitMs > delayMs) { | 
|  | 6138 | muteWaitMs -= delayMs; | 
|  | 6139 | usleep(muteWaitMs * 1000); | 
|  | 6140 | return muteWaitMs; | 
|  | 6141 | } | 
|  | 6142 | return 0; | 
|  | 6143 | } | 
|  | 6144 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6145 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, | 
|  | 6146 | const DeviceVector &devices, | 
|  | 6147 | bool force, | 
|  | 6148 | int delayMs, | 
|  | 6149 | audio_patch_handle_t *patchHandle, | 
|  | 6150 | bool requiresMuteCheck) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6151 | { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6152 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6153 | uint32_t muteWaitMs; | 
|  | 6154 |  | 
|  | 6155 | if (outputDesc->isDuplicated()) { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6156 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, | 
|  | 6157 | nullptr /* patchHandle */, requiresMuteCheck); | 
|  | 6158 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, | 
|  | 6159 | nullptr /* patchHandle */, requiresMuteCheck); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6160 | return muteWaitMs; | 
|  | 6161 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6162 |  | 
|  | 6163 | // filter devices according to output selected | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6164 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); | 
| Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 6165 | DeviceVector prevDevices = outputDesc->devices(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6166 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6167 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); | 
|  | 6168 |  | 
|  | 6169 | if (!filteredDevices.isEmpty()) { | 
|  | 6170 | outputDesc->setDevices(filteredDevices); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6171 | } | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6172 |  | 
|  | 6173 | // if the outputs are not materially active, there is no need to mute. | 
|  | 6174 | if (requiresMuteCheck) { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6175 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices, delayMs); | 
| Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 6176 | } else { | 
|  | 6177 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); | 
|  | 6178 | muteWaitMs = 0; | 
|  | 6179 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6180 |  | 
| Eric Laurent | 79ea958 | 2020-06-11 18:49:24 -0700 | [diff] [blame] | 6181 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
|  | 6182 | // output profile or if new device is not supported AND previous device(s) is(are) still | 
|  | 6183 | // available (otherwise reset device must be done on the output) | 
|  | 6184 | if (!devices.isEmpty() && filteredDevices.isEmpty() && | 
|  | 6185 | !mAvailableOutputDevices.filter(prevDevices).empty()) { | 
|  | 6186 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); | 
|  | 6187 | // restore previous device after evaluating strategy mute state | 
|  | 6188 | outputDesc->setDevices(prevDevices); | 
|  | 6189 | return muteWaitMs; | 
|  | 6190 | } | 
|  | 6191 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6192 | // Do not change the routing if: | 
| Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 6193 | //      the requested device is AUDIO_DEVICE_NONE | 
|  | 6194 | //      OR the requested device is the same as current device | 
|  | 6195 | //  AND force is not specified | 
|  | 6196 | //  AND the output is connected by a valid audio patch. | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6197 | // Doing this check here allows the caller to call setOutputDevices() without conditions | 
| Mikhail Naganov | 2d4e170 | 2019-01-24 12:59:44 -0800 | [diff] [blame] | 6198 | if ((filteredDevices.isEmpty() || filteredDevices == prevDevices) && | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6199 | !force && outputDesc->getPatchHandle() != 0) { | 
|  | 6200 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, | 
|  | 6201 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6202 | return muteWaitMs; | 
|  | 6203 | } | 
|  | 6204 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6205 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6206 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6207 | // do the routing | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6208 | if (filteredDevices.isEmpty()) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6209 | resetOutputDevice(outputDesc, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6210 | } else { | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6211 | PatchBuilder patchBuilder; | 
|  | 6212 | patchBuilder.addSource(outputDesc); | 
|  | 6213 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); | 
|  | 6214 | for (const auto &filteredDevice : filteredDevices) { | 
|  | 6215 | patchBuilder.addSink(filteredDevice); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 6216 | } | 
|  | 6217 |  | 
| Jasmine Cha | 7f82d1a | 2020-03-16 13:21:47 +0800 | [diff] [blame] | 6218 | // Add half reported latency to delayMs when muteWaitMs is null in order | 
|  | 6219 | // to avoid disordered sequence of muting volume and changing devices. | 
|  | 6220 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), | 
|  | 6221 | muteWaitMs == 0 ? (delayMs + (outputDesc->latency() / 2)) : delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6222 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6223 |  | 
|  | 6224 | // update stream volumes according to new device | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6225 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6226 |  | 
|  | 6227 | return muteWaitMs; | 
|  | 6228 | } | 
|  | 6229 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6230 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6231 | int delayMs, | 
|  | 6232 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6233 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6234 | ssize_t index; | 
|  | 6235 | if (patchHandle) { | 
|  | 6236 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 6237 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 6238 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6239 | } | 
|  | 6240 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6241 | return INVALID_OPERATION; | 
|  | 6242 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6243 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6244 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6245 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 6246 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6247 | removeAudioPatch(patchDesc->getHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6248 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 6249 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6250 | return status; | 
|  | 6251 | } | 
|  | 6252 |  | 
|  | 6253 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6254 | const sp<DeviceDescriptor> &device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6255 | bool force, | 
|  | 6256 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6257 | { | 
|  | 6258 | status_t status = NO_ERROR; | 
|  | 6259 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6260 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6261 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { | 
|  | 6262 | inputDesc->setDevice(device); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6263 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6264 | if (mAvailableInputDevices.contains(device)) { | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6265 | PatchBuilder patchBuilder; | 
|  | 6266 | patchBuilder.addSink(inputDesc, | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 6267 | // AUDIO_SOURCE_HOTWORD is for internal use only: | 
|  | 6268 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6269 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { | 
|  | 6270 | auto result = usecase; | 
|  | 6271 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { | 
|  | 6272 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
|  | 6273 | } | 
|  | 6274 | return result; }). | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6275 | //only one input device for now | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6276 | addSource(device); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6277 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6278 | } | 
|  | 6279 | } | 
|  | 6280 | return status; | 
|  | 6281 | } | 
|  | 6282 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6283 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
|  | 6284 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6285 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 6286 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6287 | ssize_t index; | 
|  | 6288 | if (patchHandle) { | 
|  | 6289 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 6290 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 6291 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6292 | } | 
|  | 6293 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6294 | return INVALID_OPERATION; | 
|  | 6295 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6296 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6297 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->getAfHandle(), 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6298 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 6299 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6300 | removeAudioPatch(patchDesc->getHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 6301 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 6302 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 6303 | return status; | 
|  | 6304 | } | 
|  | 6305 |  | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6306 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6307 | uint32_t& samplingRate, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6308 | audio_format_t& format, | 
|  | 6309 | audio_channel_mask_t& channelMask, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6310 | audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6311 | { | 
|  | 6312 | // Choose an input profile based on the requested capture parameters: select the first available | 
|  | 6313 | // profile supporting all requested parameters. | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6314 | // | 
|  | 6315 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return | 
|  | 6316 | // the best matching profile, not the first one. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6317 |  | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6318 | sp<IOProfile> firstInexact; | 
|  | 6319 | uint32_t updatedSamplingRate = 0; | 
|  | 6320 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; | 
|  | 6321 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; | 
| Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 6322 | for (const auto& hwModule : mHwModules) { | 
| Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 6323 | for (const auto& profile : hwModule->getInputProfiles()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 6324 | // profile->log(); | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6325 | //updatedFormat = format; | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6326 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6327 | &samplingRate  /*updatedSamplingRate*/, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6328 | format, | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6329 | &format,       /*updatedFormat*/ | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 6330 | channelMask, | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6331 | &channelMask   /*updatedChannelMask*/, | 
|  | 6332 | // FIXME ugly cast | 
|  | 6333 | (audio_output_flags_t) flags, | 
|  | 6334 | true /*exactMatchRequiredForInputFlags*/)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6335 | return profile; | 
|  | 6336 | } | 
| François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 6337 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6338 | samplingRate, | 
|  | 6339 | &updatedSamplingRate, | 
|  | 6340 | format, | 
|  | 6341 | &updatedFormat, | 
|  | 6342 | channelMask, | 
|  | 6343 | &updatedChannelMask, | 
|  | 6344 | // FIXME ugly cast | 
|  | 6345 | (audio_output_flags_t) flags, | 
|  | 6346 | false /*exactMatchRequiredForInputFlags*/)) { | 
|  | 6347 | firstInexact = profile; | 
|  | 6348 | } | 
|  | 6349 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6350 | } | 
|  | 6351 | } | 
| Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 6352 | if (firstInexact != nullptr) { | 
|  | 6353 | samplingRate = updatedSamplingRate; | 
|  | 6354 | format = updatedFormat; | 
|  | 6355 | channelMask = updatedChannelMask; | 
|  | 6356 | return firstInexact; | 
|  | 6357 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6358 | return NULL; | 
|  | 6359 | } | 
|  | 6360 |  | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6361 | float AudioPolicyManager::computeVolume(IVolumeCurves &curves, | 
|  | 6362 | VolumeSource volumeSource, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 6363 | int index, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6364 | const DeviceTypeSet& deviceTypes) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6365 | { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6366 | float volumeDb = curves.volIndexToDb(Volume::getDeviceCategory(deviceTypes), index); | 
| Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 6367 |  | 
|  | 6368 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much | 
|  | 6369 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch | 
|  | 6370 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to | 
|  | 6371 | // the ringtone volume | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6372 | const auto callVolumeSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL); | 
|  | 6373 | const auto ringVolumeSrc = toVolumeSource(AUDIO_STREAM_RING); | 
|  | 6374 | const auto musicVolumeSrc = toVolumeSource(AUDIO_STREAM_MUSIC); | 
|  | 6375 | const auto alarmVolumeSrc = toVolumeSource(AUDIO_STREAM_ALARM); | 
| Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6376 | const auto a11yVolumeSrc = toVolumeSource(AUDIO_STREAM_ACCESSIBILITY); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6377 |  | 
| Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6378 | if (volumeSource == a11yVolumeSrc | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6379 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) && | 
|  | 6380 | mOutputs.isActive(ringVolumeSrc, 0)) { | 
|  | 6381 | auto &ringCurves = getVolumeCurves(AUDIO_STREAM_RING); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6382 | const float ringVolumeDb = computeVolume(ringCurves, ringVolumeSrc, index, deviceTypes); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6383 | return ringVolumeDb - 4 > volumeDb ? ringVolumeDb - 4 : volumeDb; | 
| Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 6384 | } | 
|  | 6385 |  | 
| Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 6386 | // in-call: always cap volume by voice volume + some low headroom | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6387 | if ((volumeSource != callVolumeSrc && (isInCall() || | 
|  | 6388 | mOutputs.isActiveLocally(callVolumeSrc))) && | 
|  | 6389 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM) || | 
|  | 6390 | volumeSource == ringVolumeSrc || volumeSource == musicVolumeSrc || | 
|  | 6391 | volumeSource == alarmVolumeSrc || | 
|  | 6392 | volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION) || | 
|  | 6393 | volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
|  | 6394 | volumeSource == toVolumeSource(AUDIO_STREAM_DTMF) || | 
| Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6395 | volumeSource == a11yVolumeSrc)) { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6396 | auto &voiceCurves = getVolumeCurves(callVolumeSrc); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6397 | int voiceVolumeIndex = voiceCurves.getVolumeIndex(deviceTypes); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6398 | const float maxVoiceVolDb = | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6399 | computeVolume(voiceCurves, callVolumeSrc, voiceVolumeIndex, deviceTypes) | 
| Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 6400 | + IN_CALL_EARPIECE_HEADROOM_DB; | 
| Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 6401 | // FIXME: Workaround for call screening applications until a proper audio mode is defined | 
|  | 6402 | // to support this scenario : Exempt the RING stream from the audio cap if the audio was | 
|  | 6403 | // programmatically muted. | 
|  | 6404 | // VOICE_CALL stream has minVolumeIndex > 0 : Users cannot set the volume of voice calls to | 
|  | 6405 | // 0. We don't want to cap volume when the system has programmatically muted the voice call | 
|  | 6406 | // stream. See setVolumeCurveIndex() for more information. | 
| Jean-Michel Trivi | 441ed65 | 2019-07-11 14:55:16 -0700 | [diff] [blame] | 6407 | bool exemptFromCapping = | 
|  | 6408 | ((volumeSource == ringVolumeSrc) || (volumeSource == a11yVolumeSrc)) | 
|  | 6409 | && (voiceVolumeIndex == 0); | 
| Abhijith Shastry | 329ddab | 2019-04-23 11:53:26 -0700 | [diff] [blame] | 6410 | ALOGV_IF(exemptFromCapping, "%s volume source %d at vol=%f not capped", __func__, | 
|  | 6411 | volumeSource, volumeDb); | 
|  | 6412 | if ((volumeDb > maxVoiceVolDb) && !exemptFromCapping) { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6413 | ALOGV("%s volume source %d at vol=%f overriden by volume group %d at vol=%f", __func__, | 
|  | 6414 | volumeSource, volumeDb, callVolumeSrc, maxVoiceVolDb); | 
|  | 6415 | volumeDb = maxVoiceVolDb; | 
| Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 6416 | } | 
|  | 6417 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6418 | // if a headset is connected, apply the following rules to ring tones and notifications | 
|  | 6419 | // to avoid sound level bursts in user's ears: | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 6420 | // - always attenuate notifications volume by 6dB | 
|  | 6421 | // - attenuate ring tones volume by 6dB unless music is not playing and | 
|  | 6422 | // speaker is part of the select devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6423 | // - if music is playing, always limit the volume to current music volume, | 
|  | 6424 | // with a minimum threshold at -36dB so that notification is always perceived. | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6425 | if (!Intersection(deviceTypes, | 
|  | 6426 | {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, | 
|  | 6427 | AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE, | 
| Eric Laurent | c42df45 | 2020-08-07 10:51:53 -0700 | [diff] [blame] | 6428 | AUDIO_DEVICE_OUT_USB_HEADSET, AUDIO_DEVICE_OUT_HEARING_AID, | 
|  | 6429 | AUDIO_DEVICE_OUT_BLE_HEADSET}).empty() && | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6430 | ((volumeSource == alarmVolumeSrc || | 
|  | 6431 | volumeSource == ringVolumeSrc) || | 
|  | 6432 | (volumeSource == toVolumeSource(AUDIO_STREAM_NOTIFICATION)) || | 
|  | 6433 | (volumeSource == toVolumeSource(AUDIO_STREAM_SYSTEM)) || | 
|  | 6434 | ((volumeSource == toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE)) && | 
|  | 6435 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && | 
|  | 6436 | curves.canBeMuted()) { | 
|  | 6437 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6438 | // when the phone is ringing we must consider that music could have been paused just before | 
|  | 6439 | // by the music application and behave as if music was active if the last music track was | 
|  | 6440 | // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 6441 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6442 | mLimitRingtoneVolume) { | 
| François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6443 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6444 | DeviceTypeSet musicDevice = | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6445 | mEngine->getOutputDevicesForAttributes(attributes_initializer(AUDIO_USAGE_MEDIA), | 
|  | 6446 | nullptr, true /*fromCache*/).types(); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6447 | auto &musicCurves = getVolumeCurves(AUDIO_STREAM_MUSIC); | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6448 | float musicVolDb = computeVolume(musicCurves, | 
|  | 6449 | musicVolumeSrc, | 
|  | 6450 | musicCurves.getVolumeIndex(musicDevice), | 
|  | 6451 | musicDevice); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6452 | float minVolDb = (musicVolDb > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? | 
|  | 6453 | musicVolDb : SONIFICATION_HEADSET_VOLUME_MIN_DB; | 
|  | 6454 | if (volumeDb > minVolDb) { | 
|  | 6455 | volumeDb = minVolDb; | 
|  | 6456 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDb, musicVolDb); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6457 | } | 
| Eric Laurent | 7b6385c | 2021-05-12 17:55:36 +0200 | [diff] [blame] | 6458 | if (Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER | 
|  | 6459 | &&  !Intersection(deviceTypes, {AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, | 
|  | 6460 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}).empty()) { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 6461 | // on A2DP, also ensure notification volume is not too low compared to media when | 
|  | 6462 | // intended to be played | 
| François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6463 | if ((volumeDb > -96.0f) && | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6464 | (musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDb)) { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6465 | ALOGV("%s increasing volume for volume source=%d device=%s from %f to %f", | 
|  | 6466 | __func__, volumeSource, dumpDeviceTypes(deviceTypes).c_str(), volumeDb, | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6467 | musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); | 
|  | 6468 | volumeDb = musicVolDb - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 6469 | } | 
|  | 6470 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6471 | } else if ((Volume::getDeviceForVolume(deviceTypes) != AUDIO_DEVICE_OUT_SPEAKER) || | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6472 | (!(volumeSource == alarmVolumeSrc || volumeSource == ringVolumeSrc))) { | 
| François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6473 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6474 | } | 
|  | 6475 | } | 
|  | 6476 |  | 
| François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 6477 | return volumeDb; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6478 | } | 
|  | 6479 |  | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6480 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 6481 | VolumeSource fromVolumeSource, | 
|  | 6482 | VolumeSource toVolumeSource) | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6483 | { | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 6484 | if (fromVolumeSource == toVolumeSource) { | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6485 | return srcIndex; | 
|  | 6486 | } | 
| Francois Gaffie | 2ffdfce | 2019-03-12 11:26:42 +0100 | [diff] [blame] | 6487 | auto &srcCurves = getVolumeCurves(fromVolumeSource); | 
|  | 6488 | auto &dstCurves = getVolumeCurves(toVolumeSource); | 
| Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6489 | float minSrc = (float)srcCurves.getVolumeIndexMin(); | 
|  | 6490 | float maxSrc = (float)srcCurves.getVolumeIndexMax(); | 
|  | 6491 | float minDst = (float)dstCurves.getVolumeIndexMin(); | 
|  | 6492 | float maxDst = (float)dstCurves.getVolumeIndexMax(); | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6493 |  | 
| Revathi Uddaraju | fe0fb8b | 2017-07-27 17:05:37 +0800 | [diff] [blame] | 6494 | // preserve mute request or correct range | 
|  | 6495 | if (srcIndex < minSrc) { | 
|  | 6496 | if (srcIndex == 0) { | 
|  | 6497 | return 0; | 
|  | 6498 | } | 
|  | 6499 | srcIndex = minSrc; | 
|  | 6500 | } else if (srcIndex > maxSrc) { | 
|  | 6501 | srcIndex = maxSrc; | 
|  | 6502 | } | 
| Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 6503 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); | 
|  | 6504 | } | 
|  | 6505 |  | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6506 | status_t AudioPolicyManager::checkAndSetVolume(IVolumeCurves &curves, | 
|  | 6507 | VolumeSource volumeSource, | 
| Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6508 | int index, | 
|  | 6509 | const sp<AudioOutputDescriptor>& outputDesc, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6510 | DeviceTypeSet deviceTypes, | 
| Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6511 | int delayMs, | 
|  | 6512 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6513 | { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6514 | // do not change actual attributes volume if the attributes is muted | 
|  | 6515 | if (outputDesc->isMuted(volumeSource)) { | 
|  | 6516 | ALOGVV("%s: volume source %d muted count %d active=%d", __func__, volumeSource, | 
|  | 6517 | outputDesc->getMuteCount(volumeSource), outputDesc->isActive(volumeSource)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6518 | return NO_ERROR; | 
|  | 6519 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6520 | VolumeSource callVolSrc = toVolumeSource(AUDIO_STREAM_VOICE_CALL); | 
|  | 6521 | VolumeSource btScoVolSrc = toVolumeSource(AUDIO_STREAM_BLUETOOTH_SCO); | 
|  | 6522 | bool isVoiceVolSrc = callVolSrc == volumeSource; | 
|  | 6523 | bool isBtScoVolSrc = btScoVolSrc == volumeSource; | 
|  | 6524 |  | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6525 | bool isScoRequested = isScoRequestedForComm(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6526 | // 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] | 6527 | // if sco and call follow same curves, bypass forceUseForComm | 
|  | 6528 | if ((callVolSrc != btScoVolSrc) && | 
| Eric Laurent | 2517af3 | 2020-11-25 15:31:27 +0100 | [diff] [blame] | 6529 | ((isVoiceVolSrc && isScoRequested) || | 
|  | 6530 | (isBtScoVolSrc && !isScoRequested))) { | 
|  | 6531 | ALOGV("%s cannot set volume group %d volume when is%srequested for comm", __func__, | 
|  | 6532 | volumeSource, isScoRequested ? " " : "n ot "); | 
| Eric Laurent | 571ef96 | 2020-07-24 11:43:48 -0700 | [diff] [blame] | 6533 | // Do not return an error here as AudioService will always set both voice call | 
|  | 6534 | // and bluetooth SCO volumes due to stream aliasing. | 
|  | 6535 | return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6536 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6537 | if (deviceTypes.empty()) { | 
|  | 6538 | deviceTypes = outputDesc->devices().types(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6539 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6540 |  | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6541 | float volumeDb = computeVolume(curves, volumeSource, index, deviceTypes); | 
|  | 6542 | if (outputDesc->isFixedVolume(deviceTypes) || | 
| Eric Laurent | 9698a4c | 2020-10-12 17:10:23 -0700 | [diff] [blame] | 6543 | // Force VoIP volume to max for bluetooth SCO device except if muted | 
|  | 6544 | (index != 0 && (isVoiceVolSrc || isBtScoVolSrc) && | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6545 | isSingleDeviceType(deviceTypes, audio_is_bluetooth_out_sco_device))) { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 6546 | volumeDb = 0.0f; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6547 | } | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6548 | outputDesc->setVolume( | 
|  | 6549 | volumeDb, volumeSource, curves.getStreamTypes(), deviceTypes, delayMs, force); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6550 |  | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6551 | if (isVoiceVolSrc || isBtScoVolSrc) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6552 | float voiceVolume; | 
| Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 6553 | // 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] | 6554 | if (isVoiceVolSrc) { | 
|  | 6555 | voiceVolume = (float)index/(float)curves.getVolumeIndexMax(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6556 | } else { | 
| Eric Laurent | fad001d | 2019-06-11 19:17:57 -0700 | [diff] [blame] | 6557 | voiceVolume = index == 0 ? 0.0 : 1.0; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6558 | } | 
| Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 6559 | if (voiceVolume != mLastVoiceVolume) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6560 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
|  | 6561 | mLastVoiceVolume = voiceVolume; | 
|  | 6562 | } | 
|  | 6563 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6564 | return NO_ERROR; | 
|  | 6565 | } | 
|  | 6566 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6567 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6568 | const DeviceTypeSet& deviceTypes, | 
|  | 6569 | int delayMs, | 
|  | 6570 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6571 | { | 
| jiabin | cd51052 | 2020-01-22 09:40:55 -0800 | [diff] [blame] | 6572 | ALOGVV("applyStreamVolumes() for device %s", dumpDeviceTypes(deviceTypes).c_str()); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6573 | for (const auto &volumeGroup : mEngine->getVolumeGroups()) { | 
|  | 6574 | auto &curves = getVolumeCurves(toVolumeSource(volumeGroup)); | 
|  | 6575 | checkAndSetVolume(curves, toVolumeSource(volumeGroup), | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6576 | curves.getVolumeIndex(deviceTypes), | 
|  | 6577 | outputDesc, deviceTypes, delayMs, force); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6578 | } | 
|  | 6579 | } | 
|  | 6580 |  | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6581 | void AudioPolicyManager::setStrategyMute(product_strategy_t strategy, | 
|  | 6582 | bool on, | 
|  | 6583 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 6584 | int delayMs, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6585 | DeviceTypeSet deviceTypes) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6586 | { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6587 | std::vector<VolumeSource> sourcesToMute; | 
|  | 6588 | for (auto attributes: mEngine->getAllAttributesForProductStrategy(strategy)) { | 
|  | 6589 | ALOGVV("%s() attributes %s, mute %d, output ID %d", __func__, | 
|  | 6590 | toString(attributes).c_str(), on, outputDesc->getId()); | 
|  | 6591 | VolumeSource source = toVolumeSource(attributes); | 
|  | 6592 | if (std::find(begin(sourcesToMute), end(sourcesToMute), source) == end(sourcesToMute)) { | 
|  | 6593 | sourcesToMute.push_back(source); | 
|  | 6594 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6595 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6596 | for (auto source : sourcesToMute) { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6597 | setVolumeSourceMute(source, on, outputDesc, delayMs, deviceTypes); | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6598 | } | 
|  | 6599 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6600 | } | 
|  | 6601 |  | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6602 | void AudioPolicyManager::setVolumeSourceMute(VolumeSource volumeSource, | 
|  | 6603 | bool on, | 
|  | 6604 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 6605 | int delayMs, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6606 | DeviceTypeSet deviceTypes) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6607 | { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6608 | if (deviceTypes.empty()) { | 
|  | 6609 | deviceTypes = outputDesc->devices().types(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6610 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6611 | auto &curves = getVolumeCurves(volumeSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6612 | if (on) { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6613 | if (!outputDesc->isMuted(volumeSource)) { | 
| Eric Laurent | f5aa58d | 2019-02-22 18:20:11 -0800 | [diff] [blame] | 6614 | if (curves.canBeMuted() && | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6615 | (volumeSource != toVolumeSource(AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
|  | 6616 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == | 
|  | 6617 | AUDIO_POLICY_FORCE_NONE))) { | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6618 | checkAndSetVolume(curves, volumeSource, 0, outputDesc, deviceTypes, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6619 | } | 
|  | 6620 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6621 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not | 
|  | 6622 | // ignored | 
|  | 6623 | outputDesc->incMuteCount(volumeSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6624 | } else { | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6625 | if (!outputDesc->isMuted(volumeSource)) { | 
|  | 6626 | ALOGV("%s unmuting non muted attributes!", __func__); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6627 | return; | 
|  | 6628 | } | 
| François Gaffie | aaac0fd | 2018-11-22 17:56:39 +0100 | [diff] [blame] | 6629 | if (outputDesc->decMuteCount(volumeSource) == 0) { | 
|  | 6630 | checkAndSetVolume(curves, volumeSource, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6631 | curves.getVolumeIndex(deviceTypes), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 6632 | outputDesc, | 
| jiabin | 9a3361e | 2019-10-01 09:38:30 -0700 | [diff] [blame] | 6633 | deviceTypes, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 6634 | delayMs); | 
|  | 6635 | } | 
|  | 6636 | } | 
|  | 6637 | } | 
|  | 6638 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 6639 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) | 
|  | 6640 | { | 
| François Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 6641 | // has flags that map to a stream type? | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 6642 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { | 
|  | 6643 | return true; | 
|  | 6644 | } | 
|  | 6645 |  | 
|  | 6646 | // has known usage? | 
|  | 6647 | switch (paa->usage) { | 
|  | 6648 | case AUDIO_USAGE_UNKNOWN: | 
|  | 6649 | case AUDIO_USAGE_MEDIA: | 
|  | 6650 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 6651 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 6652 | case AUDIO_USAGE_ALARM: | 
|  | 6653 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 6654 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 6655 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 6656 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 6657 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 6658 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 6659 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 6660 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 6661 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 6662 | case AUDIO_USAGE_GAME: | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 6663 | case AUDIO_USAGE_VIRTUAL_SOURCE: | 
| Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 6664 | case AUDIO_USAGE_ASSISTANT: | 
| Eric Laurent | 21777f8 | 2019-12-06 18:12:06 -0800 | [diff] [blame] | 6665 | case AUDIO_USAGE_CALL_ASSISTANT: | 
| Hayden Gomes | 524159d | 2019-12-23 14:41:47 -0800 | [diff] [blame] | 6666 | case AUDIO_USAGE_EMERGENCY: | 
|  | 6667 | case AUDIO_USAGE_SAFETY: | 
|  | 6668 | case AUDIO_USAGE_VEHICLE_STATUS: | 
|  | 6669 | case AUDIO_USAGE_ANNOUNCEMENT: | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 6670 | break; | 
|  | 6671 | default: | 
|  | 6672 | return false; | 
|  | 6673 | } | 
|  | 6674 | return true; | 
|  | 6675 | } | 
|  | 6676 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 6677 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) | 
|  | 6678 | { | 
|  | 6679 | return mEngine->getForceUse(usage); | 
|  | 6680 | } | 
|  | 6681 |  | 
|  | 6682 | bool AudioPolicyManager::isInCall() | 
|  | 6683 | { | 
|  | 6684 | return isStateInCall(mEngine->getPhoneState()); | 
|  | 6685 | } | 
|  | 6686 |  | 
|  | 6687 | bool AudioPolicyManager::isStateInCall(int state) | 
|  | 6688 | { | 
|  | 6689 | return is_state_in_call(state); | 
|  | 6690 | } | 
|  | 6691 |  | 
| Eric Laurent | 74b7151 | 2019-11-06 17:21:57 -0800 | [diff] [blame] | 6692 | bool AudioPolicyManager::isCallAudioAccessible() | 
|  | 6693 | { | 
|  | 6694 | audio_mode_t mode = mEngine->getPhoneState(); | 
|  | 6695 | return (mode == AUDIO_MODE_IN_CALL) | 
|  | 6696 | || (mode == AUDIO_MODE_IN_COMMUNICATION) | 
|  | 6697 | || (mode == AUDIO_MODE_CALL_SCREEN); | 
|  | 6698 | } | 
|  | 6699 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6700 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) | 
|  | 6701 | { | 
|  | 6702 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 6703 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6704 | if (sourceDesc->isConnected() && (sourceDesc->srcDevice()->equals(deviceDesc) || | 
| Francois Gaffie | 51c9ccd | 2020-10-14 18:02:07 +0200 | [diff] [blame] | 6705 | sourceDesc->sinkDevice()->equals(deviceDesc)) | 
|  | 6706 | && !isCallRxAudioSource(sourceDesc)) { | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6707 | disconnectAudioSource(sourceDesc); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6708 | } | 
|  | 6709 | } | 
|  | 6710 |  | 
|  | 6711 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
|  | 6712 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 6713 | bool release = false; | 
|  | 6714 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++)  { | 
|  | 6715 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; | 
|  | 6716 | if (source->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 6717 | source->ext.device.type == deviceDesc->type()) { | 
|  | 6718 | release = true; | 
|  | 6719 | } | 
|  | 6720 | } | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6721 | const char *address = deviceDesc->address().c_str(); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6722 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++)  { | 
|  | 6723 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; | 
|  | 6724 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && | 
| Francois Gaffie | a0e5c99 | 2020-09-29 16:05:07 +0200 | [diff] [blame] | 6725 | sink->ext.device.type == deviceDesc->type() && | 
|  | 6726 | (strnlen(address, AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0 | 
|  | 6727 | || strncmp(sink->ext.device.address, address, | 
|  | 6728 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6729 | release = true; | 
|  | 6730 | } | 
|  | 6731 | } | 
|  | 6732 | if (release) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6733 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->getHandle()); | 
|  | 6734 | releaseAudioPatch(patchDesc->getHandle(), patchDesc->getUid()); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6735 | } | 
|  | 6736 | } | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6737 |  | 
| Francois Gaffie | ba2cf0f | 2018-12-12 16:40:25 +0100 | [diff] [blame] | 6738 | mInputs.clearSessionRoutesForDevice(deviceDesc); | 
|  | 6739 |  | 
| Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 6740 | mHwModules.cleanUpForDevice(deviceDesc); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6741 | } | 
|  | 6742 |  | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6743 | void AudioPolicyManager::modifySurroundFormats( | 
|  | 6744 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6745 | std::unordered_set<audio_format_t> enforcedSurround( | 
|  | 6746 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6747 | std::unordered_set<audio_format_t> allSurround;  // A flat set of all known surround formats | 
|  | 6748 | for (const auto& pair : mConfig.getSurroundFormats()) { | 
|  | 6749 | allSurround.insert(pair.first); | 
|  | 6750 | for (const auto& subformat : pair.second) allSurround.insert(subformat); | 
|  | 6751 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6752 |  | 
|  | 6753 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 6754 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6755 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6756 | // This is the resulting set of formats depending on the surround mode: | 
|  | 6757 | //   'all surround' = allSurround | 
|  | 6758 | //   'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] | 
|  | 6759 | //   'non-surround' = not in 'all surround' and not in 'enforced surround' | 
|  | 6760 | //   'manual surround' = mManualSurroundFormats | 
|  | 6761 | // AUTO:   formats v 'enforced surround' | 
|  | 6762 | // ALWAYS: formats v 'all surround' v 'enforced surround' | 
|  | 6763 | // NEVER:  formats ^ 'non-surround' | 
|  | 6764 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6765 |  | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6766 | std::unordered_set<audio_format_t> formatSet; | 
|  | 6767 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL | 
|  | 6768 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6769 | // formatSet is (formats ^ 'non-surround') | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6770 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6771 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6772 | formatSet.insert(*formatIter); | 
|  | 6773 | } | 
|  | 6774 | } | 
|  | 6775 | } else { | 
|  | 6776 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); | 
|  | 6777 | } | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6778 | formatsPtr->clear();  // Re-filled from the formatSet at the end. | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6779 |  | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6780 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6781 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6782 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. | 
|  | 6783 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { | 
|  | 6784 | formatSet.insert(AUDIO_FORMAT_IEC61937); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6785 | } | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6786 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS | 
|  | 6787 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { | 
|  | 6788 | formatSet.insert(allSurround.begin(), allSurround.end()); | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6789 | } | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6790 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6791 | } | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6792 | for (const auto& format : formatSet) { | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6793 | formatsPtr->push_back(format); | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6794 | } | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6795 | } | 
|  | 6796 |  | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6797 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelMaskSet *channelMasksPtr) { | 
|  | 6798 | ChannelMaskSet &channelMasks = *channelMasksPtr; | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6799 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 6800 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
|  | 6801 |  | 
|  | 6802 | // If NEVER, then remove support for channelMasks > stereo. | 
|  | 6803 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6804 | for (auto it = channelMasks.begin(); it != channelMasks.end();) { | 
|  | 6805 | audio_channel_mask_t channelMask = *it; | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6806 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6807 | ALOGV("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6808 | it = channelMasks.erase(it); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6809 | } else { | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6810 | ++it; | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6811 | } | 
|  | 6812 | } | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6813 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 | 
|  | 6814 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS | 
|  | 6815 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6816 | bool supports5dot1 = false; | 
|  | 6817 | // Are there any channel masks that can be considered "surround"? | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6818 | for (audio_channel_mask_t channelMask : channelMasks) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6819 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { | 
|  | 6820 | supports5dot1 = true; | 
|  | 6821 | break; | 
|  | 6822 | } | 
|  | 6823 | } | 
|  | 6824 | // If not then add 5.1 support. | 
|  | 6825 | if (!supports5dot1) { | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6826 | channelMasks.insert(AUDIO_CHANNEL_OUT_5POINT1); | 
| Eric Laurent | fecbceb | 2021-02-09 14:46:43 +0100 | [diff] [blame] | 6827 | 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] | 6828 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6829 | } | 
|  | 6830 | } | 
|  | 6831 |  | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6832 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6833 | audio_io_handle_t ioHandle, | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6834 | AudioProfileVector &profiles) | 
|  | 6835 | { | 
|  | 6836 | String8 reply; | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6837 | audio_devices_t device = devDesc->type(); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6838 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6839 | // Format MUST be checked first to update the list of AudioProfile | 
|  | 6840 | if (profiles.hasDynamicFormat()) { | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6841 | reply = mpClientInterface->getParameters( | 
|  | 6842 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); | 
| jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6843 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6844 | AudioParameter repliedParameters(reply); | 
|  | 6845 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6846 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6847 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); | 
|  | 6848 | return; | 
|  | 6849 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6850 | FormatVector formats = formatsFromString(reply.string()); | 
| Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 6851 | mReportedFormatsMap[devDesc] = formats; | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6852 | if (device == AUDIO_DEVICE_OUT_HDMI | 
|  | 6853 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6854 | modifySurroundFormats(devDesc, &formats); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6855 | } | 
| jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 6856 | addProfilesForFormats(profiles, formats); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6857 | } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6858 |  | 
| Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6859 | for (audio_format_t format : profiles.getSupportedFormats()) { | 
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 6860 | ChannelMaskSet channelMasks; | 
|  | 6861 | SampleRateSet samplingRates; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6862 | AudioParameter requestedParameters; | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6863 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6864 |  | 
|  | 6865 | if (profiles.hasDynamicRateFor(format)) { | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6866 | reply = mpClientInterface->getParameters( | 
|  | 6867 | ioHandle, | 
|  | 6868 | requestedParameters.toString() + ";" + | 
|  | 6869 | AudioParameter::keyStreamSupportedSamplingRates); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6870 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6871 | AudioParameter repliedParameters(reply); | 
|  | 6872 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6873 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6874 | samplingRates = samplingRatesFromString(reply.string()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6875 | } | 
|  | 6876 | } | 
|  | 6877 | if (profiles.hasDynamicChannelsFor(format)) { | 
|  | 6878 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 6879 | requestedParameters.toString() + ";" + | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6880 | AudioParameter::keyStreamSupportedChannels); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6881 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6882 | AudioParameter repliedParameters(reply); | 
|  | 6883 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6884 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6885 | channelMasks = channelMasksFromString(reply.string()); | 
| Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6886 | if (device == AUDIO_DEVICE_OUT_HDMI | 
|  | 6887 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { | 
| Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6888 | modifySurroundChannelMasks(&channelMasks); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6889 | } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6890 | } | 
|  | 6891 | } | 
| jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 6892 | addDynamicAudioProfileAndSort( | 
|  | 6893 | profiles, new AudioProfile(format, channelMasks, samplingRates)); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6894 | } | 
|  | 6895 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6896 |  | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6897 | status_t AudioPolicyManager::installPatch(const char *caller, | 
|  | 6898 | audio_patch_handle_t *patchHandle, | 
|  | 6899 | AudioIODescriptorInterface *ioDescriptor, | 
|  | 6900 | const struct audio_patch *patch, | 
|  | 6901 | int delayMs) | 
|  | 6902 | { | 
|  | 6903 | ssize_t index = mAudioPatches.indexOfKey( | 
|  | 6904 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? | 
|  | 6905 | *patchHandle : ioDescriptor->getPatchHandle()); | 
|  | 6906 | sp<AudioPatch> patchDesc; | 
|  | 6907 | status_t status = installPatch( | 
|  | 6908 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); | 
|  | 6909 | if (status == NO_ERROR) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6910 | ioDescriptor->setPatchHandle(patchDesc->getHandle()); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6911 | } | 
|  | 6912 | return status; | 
|  | 6913 | } | 
|  | 6914 |  | 
|  | 6915 | status_t AudioPolicyManager::installPatch(const char *caller, | 
|  | 6916 | ssize_t index, | 
|  | 6917 | audio_patch_handle_t *patchHandle, | 
|  | 6918 | const struct audio_patch *patch, | 
|  | 6919 | int delayMs, | 
|  | 6920 | uid_t uid, | 
|  | 6921 | sp<AudioPatch> *patchDescPtr) | 
|  | 6922 | { | 
|  | 6923 | sp<AudioPatch> patchDesc; | 
|  | 6924 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 6925 | if (index >= 0) { | 
|  | 6926 | patchDesc = mAudioPatches.valueAt(index); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6927 | afPatchHandle = patchDesc->getAfHandle(); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6928 | } | 
|  | 6929 |  | 
|  | 6930 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); | 
|  | 6931 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", | 
|  | 6932 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); | 
|  | 6933 | if (status == NO_ERROR) { | 
|  | 6934 | if (index < 0) { | 
|  | 6935 | patchDesc = new AudioPatch(patch, uid); | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6936 | addAudioPatch(patchDesc->getHandle(), patchDesc); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6937 | } else { | 
|  | 6938 | patchDesc->mPatch = *patch; | 
|  | 6939 | } | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6940 | patchDesc->setAfHandle(afPatchHandle); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6941 | if (patchHandle) { | 
| François Gaffie | afd4cea | 2019-11-18 15:50:22 +0100 | [diff] [blame] | 6942 | *patchHandle = patchDesc->getHandle(); | 
| Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6943 | } | 
|  | 6944 | nextAudioPortGeneration(); | 
|  | 6945 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 6946 | } | 
|  | 6947 | if (patchDescPtr) *patchDescPtr = patchDesc; | 
|  | 6948 | return status; | 
|  | 6949 | } | 
|  | 6950 |  | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 6951 | bool AudioPolicyManager::areAllActiveTracksRerouted(const sp<SwAudioOutputDescriptor>& output) | 
|  | 6952 | { | 
|  | 6953 | const TrackClientVector activeClients = output->getActiveClients(); | 
|  | 6954 | if (activeClients.empty()) { | 
|  | 6955 | return true; | 
|  | 6956 | } | 
|  | 6957 | ssize_t index = mAudioPatches.indexOfKey(output->getPatchHandle()); | 
|  | 6958 | if (index < 0) { | 
|  | 6959 | ALOGE("%s, no audio patch found while there are active clients on output %d", | 
|  | 6960 | __func__, output->getId()); | 
|  | 6961 | return false; | 
|  | 6962 | } | 
|  | 6963 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 6964 | DeviceVector routedDevices; | 
|  | 6965 | for (int i = 0; i < patchDesc->mPatch.num_sinks; ++i) { | 
|  | 6966 | sp<DeviceDescriptor> device = mAvailableOutputDevices.getDeviceFromId( | 
|  | 6967 | patchDesc->mPatch.sinks[i].id); | 
|  | 6968 | if (device == nullptr) { | 
|  | 6969 | ALOGE("%s, no audio device found with id(%d)", | 
|  | 6970 | __func__, patchDesc->mPatch.sinks[i].id); | 
|  | 6971 | return false; | 
|  | 6972 | } | 
|  | 6973 | routedDevices.add(device); | 
|  | 6974 | } | 
|  | 6975 | for (const auto& client : activeClients) { | 
|  | 6976 | // TODO: b/175343099 only travel the valid client | 
|  | 6977 | sp<DeviceDescriptor> preferredDevice = | 
|  | 6978 | mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()); | 
|  | 6979 | if (mEngine->getOutputDevicesForAttributes( | 
|  | 6980 | client->attributes(), preferredDevice, false) == routedDevices) { | 
|  | 6981 | return false; | 
|  | 6982 | } | 
|  | 6983 | } | 
|  | 6984 | return true; | 
|  | 6985 | } | 
|  | 6986 |  | 
|  | 6987 | sp<SwAudioOutputDescriptor> AudioPolicyManager::openOutputWithProfileAndDevice( | 
|  | 6988 | const sp<IOProfile>& profile, const DeviceVector& devices) | 
|  | 6989 | { | 
|  | 6990 | for (const auto& device : devices) { | 
|  | 6991 | // TODO: This should be checking if the profile supports the device combo. | 
|  | 6992 | if (!profile->supportsDevice(device)) { | 
|  | 6993 | return nullptr; | 
|  | 6994 | } | 
|  | 6995 | } | 
|  | 6996 | sp<SwAudioOutputDescriptor> desc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
|  | 6997 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 6998 | status_t status = desc->open(nullptr, devices, | 
|  | 6999 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); | 
|  | 7000 | if (status != NO_ERROR) { | 
|  | 7001 | return nullptr; | 
|  | 7002 | } | 
|  | 7003 |  | 
|  | 7004 | // Here is where the out_set_parameters() for card & device gets called | 
|  | 7005 | sp<DeviceDescriptor> device = devices.getDeviceForOpening(); | 
|  | 7006 | const audio_devices_t deviceType = device->type(); | 
|  | 7007 | const String8 &address = String8(device->address().c_str()); | 
|  | 7008 | if (!address.isEmpty()) { | 
|  | 7009 | char *param = audio_device_address_to_parameter(deviceType, address.c_str()); | 
|  | 7010 | mpClientInterface->setParameters(output, String8(param)); | 
|  | 7011 | free(param); | 
|  | 7012 | } | 
|  | 7013 | updateAudioProfiles(device, output, profile->getAudioProfiles()); | 
|  | 7014 | if (!profile->hasValidAudioProfile()) { | 
|  | 7015 | ALOGW("%s() missing param", __func__); | 
|  | 7016 | desc->close(); | 
|  | 7017 | return nullptr; | 
|  | 7018 | } else if (profile->hasDynamicAudioProfile()) { | 
|  | 7019 | desc->close(); | 
|  | 7020 | output = AUDIO_IO_HANDLE_NONE; | 
|  | 7021 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 7022 | profile->pickAudioProfile( | 
|  | 7023 | config.sample_rate, config.channel_mask, config.format); | 
|  | 7024 | config.offload_info.sample_rate = config.sample_rate; | 
|  | 7025 | config.offload_info.channel_mask = config.channel_mask; | 
|  | 7026 | config.offload_info.format = config.format; | 
|  | 7027 |  | 
|  | 7028 | status = desc->open(&config, devices, | 
|  | 7029 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); | 
|  | 7030 | if (status != NO_ERROR) { | 
|  | 7031 | return nullptr; | 
|  | 7032 | } | 
|  | 7033 | } | 
|  | 7034 |  | 
|  | 7035 | addOutput(output, desc); | 
|  | 7036 | if (audio_is_remote_submix_device(deviceType) && address != "0") { | 
|  | 7037 | sp<AudioPolicyMix> policyMix; | 
|  | 7038 | if (mPolicyMixes.getAudioPolicyMix(deviceType, address, policyMix) == NO_ERROR) { | 
|  | 7039 | policyMix->setOutput(desc); | 
|  | 7040 | desc->mPolicyMix = policyMix; | 
|  | 7041 | } else { | 
|  | 7042 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", | 
|  | 7043 | address.string()); | 
|  | 7044 | } | 
|  | 7045 |  | 
|  | 7046 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && hasPrimaryOutput()) { | 
|  | 7047 | // no duplicated output for direct outputs and | 
|  | 7048 | // outputs used by dynamic policy mixes | 
|  | 7049 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; | 
|  | 7050 |  | 
|  | 7051 | //TODO: configure audio effect output stage here | 
|  | 7052 |  | 
|  | 7053 | // open a duplicating output thread for the new output and the primary output | 
|  | 7054 | sp<SwAudioOutputDescriptor> dupOutputDesc = | 
|  | 7055 | new SwAudioOutputDescriptor(nullptr, mpClientInterface); | 
|  | 7056 | status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, &duplicatedOutput); | 
|  | 7057 | if (status == NO_ERROR) { | 
|  | 7058 | // add duplicated output descriptor | 
|  | 7059 | addOutput(duplicatedOutput, dupOutputDesc); | 
|  | 7060 | } else { | 
|  | 7061 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
|  | 7062 | mPrimaryOutput->mIoHandle, output); | 
|  | 7063 | desc->close(); | 
|  | 7064 | removeOutput(output); | 
|  | 7065 | nextAudioPortGeneration(); | 
|  | 7066 | return nullptr; | 
|  | 7067 | } | 
|  | 7068 | } | 
| Francois Gaffie | bce7cd4 | 2020-10-14 16:13:20 +0200 | [diff] [blame] | 7069 | if (mPrimaryOutput == nullptr && profile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
|  | 7070 | ALOGV("%s(): re-assigning mPrimaryOutput", __func__); | 
|  | 7071 | mPrimaryOutput = desc; | 
|  | 7072 | } | 
| jiabin | bce0c1d | 2020-10-05 11:20:18 -0700 | [diff] [blame] | 7073 | return desc; | 
|  | 7074 | } | 
|  | 7075 |  | 
| Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 7076 | } // namespace android |