Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2014, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #define LOG_TAG "AudioFlinger::PatchPanel" |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include "Configuration.h" |
| 23 | #include <utils/Log.h> |
| 24 | #include <audio_utils/primitives.h> |
| 25 | |
| 26 | #include "AudioFlinger.h" |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 27 | #include <media/AudioParameter.h> |
Ytai Ben-Tsvi | 5385847 | 2020-11-30 11:04:46 -0800 | [diff] [blame] | 28 | #include <media/AudioValidator.h> |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 29 | #include <media/DeviceDescriptorBase.h> |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 30 | #include <media/PatchBuilder.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 31 | #include <mediautils/ServiceUtilities.h> |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
| 35 | // Note: the following macro is used for extremely verbose logging message. In |
| 36 | // order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to |
| 37 | // 0; but one side effect of this is to turn all LOGV's as well. Some messages |
| 38 | // are so verbose that we want to suppress them even when we have ALOG_ASSERT |
| 39 | // turned on. Do not uncomment the #def below unless you really know what you |
| 40 | // are doing and want to see all of the extremely verbose messages. |
| 41 | //#define VERY_VERY_VERBOSE_LOGGING |
| 42 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 43 | #define ALOGVV ALOGV |
| 44 | #else |
| 45 | #define ALOGVV(a...) do { } while(0) |
| 46 | #endif |
| 47 | |
| 48 | namespace android { |
| 49 | |
| 50 | /* List connected audio ports and their attributes */ |
| 51 | status_t AudioFlinger::listAudioPorts(unsigned int *num_ports, |
| 52 | struct audio_port *ports) |
| 53 | { |
| 54 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 55 | return mPatchPanel.listAudioPorts(num_ports, ports); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Get supported attributes for a given audio port */ |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 59 | status_t AudioFlinger::getAudioPort(struct audio_port_v7 *port) { |
Ytai Ben-Tsvi | 5385847 | 2020-11-30 11:04:46 -0800 | [diff] [blame] | 60 | status_t status = AudioValidator::validateAudioPort(*port); |
| 61 | if (status != NO_ERROR) { |
| 62 | return status; |
| 63 | } |
| 64 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 65 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 66 | return mPatchPanel.getAudioPort(port); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 69 | /* Connect a patch between several source and sink ports */ |
| 70 | status_t AudioFlinger::createAudioPatch(const struct audio_patch *patch, |
| 71 | audio_patch_handle_t *handle) |
| 72 | { |
Ytai Ben-Tsvi | 5385847 | 2020-11-30 11:04:46 -0800 | [diff] [blame] | 73 | status_t status = AudioValidator::validateAudioPatch(*patch); |
| 74 | if (status != NO_ERROR) { |
| 75 | return status; |
| 76 | } |
| 77 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 78 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 79 | return mPatchPanel.createAudioPatch(patch, handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* Disconnect a patch */ |
| 83 | status_t AudioFlinger::releaseAudioPatch(audio_patch_handle_t handle) |
| 84 | { |
| 85 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 86 | return mPatchPanel.releaseAudioPatch(handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 89 | /* List connected audio ports and they attributes */ |
| 90 | status_t AudioFlinger::listAudioPatches(unsigned int *num_patches, |
| 91 | struct audio_patch *patches) |
| 92 | { |
| 93 | Mutex::Autolock _l(mLock); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 94 | return mPatchPanel.listAudioPatches(num_patches, patches); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 97 | status_t AudioFlinger::PatchPanel::SoftwarePatch::getLatencyMs_l(double *latencyMs) const |
| 98 | { |
| 99 | const auto& iter = mPatchPanel.mPatches.find(mPatchHandle); |
| 100 | if (iter != mPatchPanel.mPatches.end()) { |
| 101 | return iter->second.getLatencyMs(latencyMs); |
| 102 | } else { |
| 103 | return BAD_VALUE; |
| 104 | } |
| 105 | } |
| 106 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 107 | /* List connected audio ports and their attributes */ |
| 108 | status_t AudioFlinger::PatchPanel::listAudioPorts(unsigned int *num_ports __unused, |
| 109 | struct audio_port *ports __unused) |
| 110 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 111 | ALOGV(__func__); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 112 | return NO_ERROR; |
| 113 | } |
| 114 | |
| 115 | /* Get supported attributes for a given audio port */ |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 116 | status_t AudioFlinger::PatchPanel::getAudioPort(struct audio_port_v7 *port) |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 117 | { |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 118 | if (port->type != AUDIO_PORT_TYPE_DEVICE) { |
| 119 | // Only query the HAL when the port is a device. |
| 120 | // TODO: implement getAudioPort for mix. |
| 121 | return INVALID_OPERATION; |
| 122 | } |
| 123 | AudioHwDevice* hwDevice = findAudioHwDeviceByModule(port->ext.device.hw_module); |
| 124 | if (hwDevice == nullptr) { |
| 125 | ALOGW("%s cannot find hw module %d", __func__, port->ext.device.hw_module); |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | if (!hwDevice->supportsAudioPatches()) { |
| 129 | return INVALID_OPERATION; |
| 130 | } |
| 131 | return hwDevice->getAudioPort(port); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 134 | /* Connect a patch between several source and sink ports */ |
| 135 | status_t AudioFlinger::PatchPanel::createAudioPatch(const struct audio_patch *patch, |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 136 | audio_patch_handle_t *handle, |
| 137 | bool endpointPatch) |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 138 | //unlocks AudioFlinger::mLock when calling ThreadBase::sendCreateAudioPatchConfigEvent |
| 139 | //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock |
| 140 | //before processing the create patch request. |
| 141 | NO_THREAD_SAFETY_ANALYSIS |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 142 | { |
Greg Kaiser | f27ce40 | 2016-03-14 13:43:14 -0700 | [diff] [blame] | 143 | if (handle == NULL || patch == NULL) { |
| 144 | return BAD_VALUE; |
| 145 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 146 | ALOGV("%s() num_sources %d num_sinks %d handle %d", |
| 147 | __func__, patch->num_sources, patch->num_sinks, *handle); |
| 148 | status_t status = NO_ERROR; |
| 149 | audio_patch_handle_t halHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 150 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 151 | if (!audio_patch_is_valid(patch) || (patch->num_sinks == 0 && patch->num_sources != 2)) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 152 | return BAD_VALUE; |
| 153 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 154 | // limit number of sources to 1 for now or 2 sources for special cross hw module case. |
| 155 | // only the audio policy manager can request a patch creation with 2 sources. |
| 156 | if (patch->num_sources > 2) { |
| 157 | return INVALID_OPERATION; |
| 158 | } |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 159 | bool reuseExistingHalPatch = false; |
| 160 | audio_patch_handle_t oldhandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 161 | if (*handle != AUDIO_PATCH_HANDLE_NONE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 162 | auto iter = mPatches.find(*handle); |
| 163 | if (iter != mPatches.end()) { |
| 164 | ALOGV("%s() removing patch handle %d", __func__, *handle); |
| 165 | Patch &removedPatch = iter->second; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 166 | // free resources owned by the removed patch if applicable |
| 167 | // 1) if a software patch is present, release the playback and capture threads and |
| 168 | // tracks created. This will also release the corresponding audio HAL patches |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 169 | if (removedPatch.isSoftware()) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 170 | removedPatch.clearConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 171 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 172 | // 2) if the new patch and old patch source or sink are devices from different |
| 173 | // hw modules, clear the audio HAL patches now because they will not be updated |
| 174 | // by call to create_audio_patch() below which will happen on a different HW module |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 175 | if (removedPatch.mHalHandle != AUDIO_PATCH_HANDLE_NONE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 176 | audio_module_handle_t hwModule = AUDIO_MODULE_HANDLE_NONE; |
| 177 | const struct audio_patch &oldPatch = removedPatch.mAudioPatch; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 178 | oldhandle = *handle; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 179 | if (oldPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 180 | (patch->sources[0].type != AUDIO_PORT_TYPE_DEVICE || |
| 181 | oldPatch.sources[0].ext.device.hw_module != |
| 182 | patch->sources[0].ext.device.hw_module)) { |
| 183 | hwModule = oldPatch.sources[0].ext.device.hw_module; |
| 184 | } else if (patch->num_sinks == 0 || |
| 185 | (oldPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE && |
| 186 | (patch->sinks[0].type != AUDIO_PORT_TYPE_DEVICE || |
| 187 | oldPatch.sinks[0].ext.device.hw_module != |
| 188 | patch->sinks[0].ext.device.hw_module))) { |
| 189 | // Note on (patch->num_sinks == 0): this situation should not happen as |
| 190 | // these special patches are only created by the policy manager but just |
| 191 | // in case, systematically clear the HAL patch. |
| 192 | // Note that removedPatch.mAudioPatch.num_sinks cannot be 0 here because |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 193 | // removedPatch.mHalHandle would be AUDIO_PATCH_HANDLE_NONE in this case. |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 194 | hwModule = oldPatch.sinks[0].ext.device.hw_module; |
| 195 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 196 | sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(hwModule); |
| 197 | if (hwDevice != 0) { |
| 198 | hwDevice->releaseAudioPatch(removedPatch.mHalHandle); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 199 | } |
Eric Laurent | 9bcfa7c | 2019-11-21 15:45:04 -0800 | [diff] [blame] | 200 | halHandle = removedPatch.mHalHandle; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 201 | // Prevent to remove/add device effect when mix / device did not change, and |
| 202 | // hal patch has not been released |
| 203 | // Note that no patch leak at hal layer as halHandle is reused. |
| 204 | reuseExistingHalPatch = (hwDevice == 0) && patchesHaveSameRoute(*patch, oldPatch); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 205 | } |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 206 | erasePatch(*handle, reuseExistingHalPatch); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 210 | Patch newPatch{*patch, endpointPatch}; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 211 | audio_module_handle_t insertedModule = AUDIO_MODULE_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 212 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 213 | switch (patch->sources[0].type) { |
| 214 | case AUDIO_PORT_TYPE_DEVICE: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 215 | audio_module_handle_t srcModule = patch->sources[0].ext.device.hw_module; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 216 | AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(srcModule); |
| 217 | if (!audioHwDevice) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 218 | status = BAD_VALUE; |
| 219 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 220 | } |
| 221 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 222 | // support only one sink if connection to a mix or across HW modules |
| 223 | if ((patch->sinks[i].type == AUDIO_PORT_TYPE_MIX || |
Mikhail Naganov | c589a49 | 2018-05-16 11:14:57 -0700 | [diff] [blame] | 224 | (patch->sinks[i].type == AUDIO_PORT_TYPE_DEVICE && |
| 225 | patch->sinks[i].ext.device.hw_module != srcModule)) && |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 226 | patch->num_sinks > 1) { |
Mikhail Naganov | c589a49 | 2018-05-16 11:14:57 -0700 | [diff] [blame] | 227 | ALOGW("%s() multiple sinks for mix or across modules not supported", __func__); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 228 | status = INVALID_OPERATION; |
| 229 | goto exit; |
| 230 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 231 | // reject connection to different sink types |
| 232 | if (patch->sinks[i].type != patch->sinks[0].type) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 233 | ALOGW("%s() different sink types in same patch not supported", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 234 | status = BAD_VALUE; |
| 235 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 236 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 239 | // manage patches requiring a software bridge |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 240 | // - special patch request with 2 sources (reuse one existing output mix) OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 241 | // - Device to device AND |
| 242 | // - source HW module != destination HW module OR |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 243 | // - audio HAL does not support audio patches creation |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 244 | if ((patch->num_sources == 2) || |
| 245 | ((patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) && |
| 246 | ((patch->sinks[0].ext.device.hw_module != srcModule) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 247 | !audioHwDevice->supportsAudioPatches()))) { |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 248 | audio_devices_t outputDevice = patch->sinks[0].ext.device.type; |
| 249 | String8 outputDeviceAddress = String8(patch->sinks[0].ext.device.address); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 250 | if (patch->num_sources == 2) { |
| 251 | if (patch->sources[1].type != AUDIO_PORT_TYPE_MIX || |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 252 | (patch->num_sinks != 0 && patch->sinks[0].ext.device.hw_module != |
| 253 | patch->sources[1].ext.mix.hw_module)) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 254 | ALOGW("%s() invalid source combination", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 255 | status = INVALID_OPERATION; |
| 256 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 257 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 258 | sp<ThreadBase> thread = |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 259 | mAudioFlinger.checkPlaybackThread_l(patch->sources[1].ext.mix.handle); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 260 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 261 | ALOGW("%s() cannot get playback thread", __func__); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 262 | status = INVALID_OPERATION; |
| 263 | goto exit; |
| 264 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 265 | // existing playback thread is reused, so it is not closed when patch is cleared |
| 266 | newPatch.mPlayback.setThread( |
| 267 | reinterpret_cast<PlaybackThread*>(thread.get()), false /*closeThread*/); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 268 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 269 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 270 | audio_config_base_t mixerConfig = AUDIO_CONFIG_BASE_INITIALIZER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 271 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Mikhail Naganov | 67bae2c | 2018-07-16 15:44:35 -0700 | [diff] [blame] | 272 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 273 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 274 | config.sample_rate = patch->sinks[0].sample_rate; |
| 275 | } |
| 276 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 277 | config.channel_mask = patch->sinks[0].channel_mask; |
| 278 | } |
| 279 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 280 | config.format = patch->sinks[0].format; |
| 281 | } |
| 282 | if (patch->sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS) { |
| 283 | flags = patch->sinks[0].flags.output; |
| 284 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 285 | sp<ThreadBase> thread = mAudioFlinger.openOutput_l( |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 286 | patch->sinks[0].ext.device.hw_module, |
| 287 | &output, |
| 288 | &config, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 289 | &mixerConfig, |
juyuchen | 2224c5a | 2019-01-21 12:00:58 +0800 | [diff] [blame] | 290 | outputDevice, |
| 291 | outputDeviceAddress, |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 292 | flags); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 293 | ALOGV("mAudioFlinger.openOutput_l() returned %p", thread.get()); |
| 294 | if (thread == 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 295 | status = NO_MEMORY; |
| 296 | goto exit; |
| 297 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 298 | newPatch.mPlayback.setThread(reinterpret_cast<PlaybackThread*>(thread.get())); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 299 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 300 | audio_devices_t device = patch->sources[0].ext.device.type; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 301 | String8 address = String8(patch->sources[0].ext.device.address); |
| 302 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 303 | // open input stream with source device audio properties if provided or |
| 304 | // default to peer output stream properties otherwise. |
| 305 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_SAMPLE_RATE) { |
| 306 | config.sample_rate = patch->sources[0].sample_rate; |
| 307 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 308 | config.sample_rate = newPatch.mPlayback.thread()->sampleRate(); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 309 | } |
| 310 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_CHANNEL_MASK) { |
| 311 | config.channel_mask = patch->sources[0].channel_mask; |
| 312 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 313 | config.channel_mask = audio_channel_in_mask_from_count( |
| 314 | newPatch.mPlayback.thread()->channelCount()); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 315 | } |
| 316 | if (patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FORMAT) { |
| 317 | config.format = patch->sources[0].format; |
| 318 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 319 | config.format = newPatch.mPlayback.thread()->format(); |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 320 | } |
Mikhail Naganov | 32abc2b | 2018-05-24 12:57:11 -0700 | [diff] [blame] | 321 | audio_input_flags_t flags = |
| 322 | patch->sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 323 | patch->sources[0].flags.input : AUDIO_INPUT_FLAG_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 324 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 325 | audio_source_t source = AUDIO_SOURCE_MIC; |
| 326 | // For telephony patches, propagate voice communication use case to record side |
| 327 | if (patch->num_sources == 2 |
| 328 | && patch->sources[1].ext.mix.usecase.stream |
| 329 | == AUDIO_STREAM_VOICE_CALL) { |
| 330 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 331 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 332 | sp<ThreadBase> thread = mAudioFlinger.openInput_l(srcModule, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 333 | &input, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 334 | &config, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 335 | device, |
| 336 | address, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 337 | source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 338 | flags, |
| 339 | outputDevice, |
| 340 | outputDeviceAddress); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 341 | ALOGV("mAudioFlinger.openInput_l() returned %p inChannelMask %08x", |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 342 | thread.get(), config.channel_mask); |
| 343 | if (thread == 0) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 344 | status = NO_MEMORY; |
| 345 | goto exit; |
| 346 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 347 | newPatch.mRecord.setThread(reinterpret_cast<RecordThread*>(thread.get())); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 348 | status = newPatch.createConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 349 | if (status != NO_ERROR) { |
| 350 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 351 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 352 | if (audioHwDevice->isInsert()) { |
| 353 | insertedModule = audioHwDevice->handle(); |
| 354 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 355 | } else { |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 356 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 357 | sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l( |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 358 | patch->sinks[0].ext.mix.handle); |
| 359 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 360 | thread = mAudioFlinger.checkMmapThread_l(patch->sinks[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 361 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 362 | ALOGW("%s() bad capture I/O handle %d", |
| 363 | __func__, patch->sinks[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 364 | status = BAD_VALUE; |
| 365 | goto exit; |
| 366 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 367 | } |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 368 | mAudioFlinger.unlock(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 369 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 370 | mAudioFlinger.lock(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 371 | if (status == NO_ERROR) { |
| 372 | newPatch.setThread(thread); |
| 373 | } |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 374 | // remove stale audio patch with same input as sink if any |
| 375 | for (auto& iter : mPatches) { |
| 376 | if (iter.second.mAudioPatch.sinks[0].ext.mix.handle == thread->id()) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 377 | erasePatch(iter.first); |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 381 | } else { |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 382 | sp<DeviceHalInterface> hwDevice = audioHwDevice->hwDevice(); |
| 383 | status = hwDevice->createAudioPatch(patch->num_sources, |
| 384 | patch->sources, |
| 385 | patch->num_sinks, |
| 386 | patch->sinks, |
| 387 | &halHandle); |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 388 | if (status == INVALID_OPERATION) goto exit; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 389 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 390 | } |
| 391 | } break; |
| 392 | case AUDIO_PORT_TYPE_MIX: { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 393 | audio_module_handle_t srcModule = patch->sources[0].ext.mix.hw_module; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 394 | ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(srcModule); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 395 | if (index < 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 396 | ALOGW("%s() bad src hw module %d", __func__, srcModule); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 397 | status = BAD_VALUE; |
| 398 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 399 | } |
| 400 | // limit to connections between devices and output streams |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 401 | DeviceDescriptorBaseVector devices; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 402 | for (unsigned int i = 0; i < patch->num_sinks; i++) { |
| 403 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 404 | ALOGW("%s() invalid sink type %d for mix source", |
| 405 | __func__, patch->sinks[i].type); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 406 | status = BAD_VALUE; |
| 407 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 408 | } |
| 409 | // limit to connections between sinks and sources on same HW module |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 410 | if (patch->sinks[i].ext.device.hw_module != srcModule) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 411 | status = BAD_VALUE; |
| 412 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 413 | } |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 414 | sp<DeviceDescriptorBase> device = new DeviceDescriptorBase( |
| 415 | patch->sinks[i].ext.device.type); |
| 416 | device->setAddress(patch->sinks[i].ext.device.address); |
| 417 | device->applyAudioPortConfig(&patch->sinks[i]); |
| 418 | devices.push_back(device); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 419 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 420 | sp<ThreadBase> thread = |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 421 | mAudioFlinger.checkPlaybackThread_l(patch->sources[0].ext.mix.handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 422 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 423 | thread = mAudioFlinger.checkMmapThread_l(patch->sources[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 424 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 425 | ALOGW("%s() bad playback I/O handle %d", |
| 426 | __func__, patch->sources[0].ext.mix.handle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 427 | status = BAD_VALUE; |
| 428 | goto exit; |
| 429 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 430 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 431 | if (thread == mAudioFlinger.primaryPlaybackThread_l()) { |
jiabin | c52b1ff | 2019-10-31 17:20:42 -0700 | [diff] [blame] | 432 | mAudioFlinger.updateOutDevicesForRecordThreads_l(devices); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 435 | mAudioFlinger.unlock(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 436 | status = thread->sendCreateAudioPatchConfigEvent(patch, &halHandle); |
Eric Laurent | 1e28aaa | 2023-04-16 19:34:23 +0200 | [diff] [blame] | 437 | mAudioFlinger.lock(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 438 | if (status == NO_ERROR) { |
| 439 | newPatch.setThread(thread); |
| 440 | } |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 441 | |
| 442 | // remove stale audio patch with same output as source if any |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 443 | // Prevent to remove endpoint patches (involved in a SwBridge) |
| 444 | // Prevent to remove AudioPatch used to route an output involved in an endpoint. |
| 445 | if (!endpointPatch) { |
| 446 | for (auto& iter : mPatches) { |
| 447 | if (iter.second.mAudioPatch.sources[0].ext.mix.handle == thread->id() && |
| 448 | !iter.second.mIsEndpointPatch) { |
| 449 | erasePatch(iter.first); |
| 450 | break; |
| 451 | } |
Eric Laurent | 526aa57 | 2019-01-15 10:54:58 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 454 | } break; |
| 455 | default: |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 456 | status = BAD_VALUE; |
| 457 | goto exit; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 458 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 459 | exit: |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 460 | ALOGV("%s() status %d", __func__, status); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 461 | if (status == NO_ERROR) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 462 | *handle = (audio_patch_handle_t) mAudioFlinger.nextUniqueId(AUDIO_UNIQUE_ID_USE_PATCH); |
| 463 | newPatch.mHalHandle = halHandle; |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 464 | if (reuseExistingHalPatch) { |
| 465 | mAudioFlinger.mPatchCommandThread->updateAudioPatch(oldhandle, *handle, newPatch); |
| 466 | } else { |
| 467 | mAudioFlinger.mPatchCommandThread->createAudioPatch(*handle, newPatch); |
| 468 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 469 | if (insertedModule != AUDIO_MODULE_HANDLE_NONE) { |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 470 | addSoftwarePatchToInsertedModules(insertedModule, *handle, &newPatch.mAudioPatch); |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 471 | } |
Eric Laurent | ef03eef | 2021-01-05 16:30:04 +0100 | [diff] [blame] | 472 | mPatches.insert(std::make_pair(*handle, std::move(newPatch))); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 473 | } else { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 474 | newPatch.clearConnections(this); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 475 | } |
| 476 | return status; |
| 477 | } |
| 478 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 479 | AudioFlinger::PatchPanel::Patch::~Patch() |
| 480 | { |
| 481 | ALOGE_IF(isSoftware(), "Software patch connections leaked %d %d", |
| 482 | mRecord.handle(), mPlayback.handle()); |
| 483 | } |
| 484 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 485 | status_t AudioFlinger::PatchPanel::Patch::createConnections(PatchPanel *panel) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 486 | { |
| 487 | // create patch from source device to record thread input |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 488 | status_t status = panel->createAudioPatch( |
| 489 | PatchBuilder().addSource(mAudioPatch.sources[0]). |
| 490 | addSink(mRecord.thread(), { .source = AUDIO_SOURCE_MIC }).patch(), |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 491 | mRecord.handlePtr(), |
| 492 | true /*endpointPatch*/); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 493 | if (status != NO_ERROR) { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 494 | *mRecord.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 495 | return status; |
| 496 | } |
| 497 | |
| 498 | // create patch from playback thread output to sink device |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 499 | if (mAudioPatch.num_sinks != 0) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 500 | status = panel->createAudioPatch( |
| 501 | PatchBuilder().addSource(mPlayback.thread()).addSink(mAudioPatch.sinks[0]).patch(), |
Francois Gaffie | e0dc36d | 2021-04-01 16:01:00 +0200 | [diff] [blame] | 502 | mPlayback.handlePtr(), |
| 503 | true /*endpointPatch*/); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 504 | if (status != NO_ERROR) { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 505 | *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 506 | return status; |
| 507 | } |
| 508 | } else { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 509 | *mPlayback.handlePtr() = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 512 | // create a special record track to capture from record thread |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 513 | uint32_t channelCount = mPlayback.thread()->channelCount(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 514 | audio_channel_mask_t inChannelMask = audio_channel_in_mask_from_count(channelCount); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 515 | audio_channel_mask_t outChannelMask = mPlayback.thread()->channelMask(); |
| 516 | uint32_t sampleRate = mPlayback.thread()->sampleRate(); |
| 517 | audio_format_t format = mPlayback.thread()->format(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 518 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 519 | audio_format_t inputFormat = mRecord.thread()->format(); |
| 520 | if (!audio_is_linear_pcm(inputFormat)) { |
| 521 | // The playbackThread format will say PCM for IEC61937 packetized stream. |
| 522 | // Use recordThread format. |
| 523 | format = inputFormat; |
| 524 | } |
| 525 | audio_input_flags_t inputFlags = mAudioPatch.sources[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 526 | mAudioPatch.sources[0].flags.input : AUDIO_INPUT_FLAG_NONE; |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 527 | if (sampleRate == mRecord.thread()->sampleRate() && |
| 528 | inChannelMask == mRecord.thread()->channelMask() && |
| 529 | mRecord.thread()->fastTrackAvailable() && |
| 530 | mRecord.thread()->hasFastCapture()) { |
| 531 | // Create a fast track if the record thread has fast capture to get better performance. |
| 532 | // Only enable fast mode when there is no resample needed. |
| 533 | inputFlags = (audio_input_flags_t) (inputFlags | AUDIO_INPUT_FLAG_FAST); |
| 534 | } else { |
| 535 | // Fast mode is not available in this case. |
| 536 | inputFlags = (audio_input_flags_t) (inputFlags & ~AUDIO_INPUT_FLAG_FAST); |
| 537 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 538 | |
Mikhail Naganov | 7c6ae98 | 2018-06-14 12:33:38 -0700 | [diff] [blame] | 539 | audio_output_flags_t outputFlags = mAudioPatch.sinks[0].config_mask & AUDIO_PORT_CONFIG_FLAGS ? |
| 540 | mAudioPatch.sinks[0].flags.output : AUDIO_OUTPUT_FLAG_NONE; |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 541 | audio_stream_type_t streamType = AUDIO_STREAM_PATCH; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 542 | audio_source_t source = AUDIO_SOURCE_DEFAULT; |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 543 | if (mAudioPatch.num_sources == 2 && mAudioPatch.sources[1].type == AUDIO_PORT_TYPE_MIX) { |
| 544 | // "reuse one existing output mix" case |
| 545 | streamType = mAudioPatch.sources[1].ext.mix.usecase.stream; |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 546 | // For telephony patches, propagate voice communication use case to record side |
| 547 | if (streamType == AUDIO_STREAM_VOICE_CALL) { |
| 548 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 549 | } |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 550 | } |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 551 | if (mPlayback.thread()->hasFastMixer()) { |
| 552 | // Create a fast track if the playback thread has fast mixer to get better performance. |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 553 | // Note: we should have matching channel mask, sample rate, and format by the logic above. |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 554 | outputFlags = (audio_output_flags_t) (outputFlags | AUDIO_OUTPUT_FLAG_FAST); |
Andy Hung | ae22b48 | 2019-05-09 15:38:55 -0700 | [diff] [blame] | 555 | } else { |
| 556 | outputFlags = (audio_output_flags_t) (outputFlags & ~AUDIO_OUTPUT_FLAG_FAST); |
jiabin | 01c8f56 | 2018-07-19 17:47:28 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 559 | sp<RecordThread::PatchRecord> tempRecordTrack; |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame] | 560 | const bool usePassthruPatchRecord = |
| 561 | (inputFlags & AUDIO_INPUT_FLAG_DIRECT) && (outputFlags & AUDIO_OUTPUT_FLAG_DIRECT); |
Dean Wheatley | b364389 | 2019-12-18 08:38:37 +1100 | [diff] [blame] | 562 | const size_t playbackFrameCount = mPlayback.thread()->frameCount(); |
| 563 | const size_t recordFrameCount = mRecord.thread()->frameCount(); |
| 564 | size_t frameCount = 0; |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame] | 565 | if (usePassthruPatchRecord) { |
Dean Wheatley | b364389 | 2019-12-18 08:38:37 +1100 | [diff] [blame] | 566 | // PassthruPatchRecord producesBufferOnDemand, so use |
| 567 | // maximum of playback and record thread framecounts |
| 568 | frameCount = std::max(playbackFrameCount, recordFrameCount); |
| 569 | ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu", |
| 570 | __func__, playbackFrameCount, recordFrameCount, frameCount); |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 571 | tempRecordTrack = new RecordThread::PassthruPatchRecord( |
| 572 | mRecord.thread().get(), |
| 573 | sampleRate, |
| 574 | inChannelMask, |
| 575 | format, |
| 576 | frameCount, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 577 | inputFlags, |
| 578 | source); |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 579 | } else { |
Dean Wheatley | b364389 | 2019-12-18 08:38:37 +1100 | [diff] [blame] | 580 | // use a pseudo LCM between input and output framecount |
| 581 | int playbackShift = __builtin_ctz(playbackFrameCount); |
| 582 | int shift = __builtin_ctz(recordFrameCount); |
| 583 | if (playbackShift < shift) { |
| 584 | shift = playbackShift; |
| 585 | } |
| 586 | frameCount = (playbackFrameCount * recordFrameCount) >> shift; |
| 587 | ALOGV("%s() playframeCount %zu recordFrameCount %zu frameCount %zu", |
| 588 | __func__, playbackFrameCount, recordFrameCount, frameCount); |
| 589 | |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 590 | tempRecordTrack = new RecordThread::PatchRecord( |
| 591 | mRecord.thread().get(), |
| 592 | sampleRate, |
| 593 | inChannelMask, |
| 594 | format, |
| 595 | frameCount, |
| 596 | nullptr, |
| 597 | (size_t)0 /* bufferSize */, |
Eric Laurent | 78b0730 | 2022-10-07 16:20:34 +0200 | [diff] [blame] | 598 | inputFlags, |
| 599 | {} /* timeout */, |
| 600 | source); |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 601 | } |
| 602 | status = mRecord.checkTrack(tempRecordTrack.get()); |
| 603 | if (status != NO_ERROR) { |
| 604 | return status; |
| 605 | } |
| 606 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 607 | // create a special playback track to render to playback thread. |
| 608 | // this track is given the same buffer as the PatchRecord buffer |
Francois Gaffie | a0fd4c7 | 2021-05-05 10:49:17 +0200 | [diff] [blame] | 609 | |
| 610 | // Default behaviour is to start as soon as possible to have the lowest possible latency even if |
| 611 | // it might glitch. |
| 612 | // Disable this behavior for FM Tuner source if no fast capture/mixer available. |
| 613 | const bool isFmBridge = mAudioPatch.sources[0].ext.device.type == AUDIO_DEVICE_IN_FM_TUNER; |
| 614 | const size_t frameCountToBeReady = isFmBridge && !usePassthruPatchRecord ? frameCount / 4 : 1; |
Mikhail Naganov | 9515fc8 | 2019-10-01 16:52:14 -0700 | [diff] [blame] | 615 | sp<PlaybackThread::PatchTrack> tempPatchTrack = new PlaybackThread::PatchTrack( |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 616 | mPlayback.thread().get(), |
Mikhail Naganov | 776eb21 | 2018-07-19 15:14:11 -0700 | [diff] [blame] | 617 | streamType, |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 618 | sampleRate, |
| 619 | outChannelMask, |
| 620 | format, |
| 621 | frameCount, |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 622 | tempRecordTrack->buffer(), |
| 623 | tempRecordTrack->bufferSize(), |
Francois Gaffie | a0fd4c7 | 2021-05-05 10:49:17 +0200 | [diff] [blame] | 624 | outputFlags, |
| 625 | {} /*timeout*/, |
| 626 | frameCountToBeReady); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 627 | status = mPlayback.checkTrack(tempPatchTrack.get()); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 628 | if (status != NO_ERROR) { |
| 629 | return status; |
| 630 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 631 | |
| 632 | // tie playback and record tracks together |
Mikhail Naganov | dd91ce2 | 2020-01-13 11:34:30 -0800 | [diff] [blame] | 633 | // In the case of PassthruPatchRecord no I/O activity happens on RecordThread, |
| 634 | // everything is driven from PlaybackThread. Thus AudioBufferProvider methods |
| 635 | // of PassthruPatchRecord can only be called if the corresponding PatchTrack |
| 636 | // is alive. There is no need to hold a reference, and there is no need |
| 637 | // to clear it. In fact, since playback stopping is asynchronous, there is |
| 638 | // no proper time when clearing could be done. |
| 639 | mRecord.setTrackAndPeer(tempRecordTrack, tempPatchTrack, !usePassthruPatchRecord); |
| 640 | mPlayback.setTrackAndPeer(tempPatchTrack, tempRecordTrack, true /*holdReference*/); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 641 | |
| 642 | // start capture and playback |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 643 | mRecord.track()->start(AudioSystem::SYNC_EVENT_NONE, AUDIO_SESSION_NONE); |
| 644 | mPlayback.track()->start(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 645 | |
| 646 | return status; |
| 647 | } |
| 648 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 649 | void AudioFlinger::PatchPanel::Patch::clearConnections(PatchPanel *panel) |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 650 | { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 651 | ALOGV("%s() mRecord.handle %d mPlayback.handle %d", |
| 652 | __func__, mRecord.handle(), mPlayback.handle()); |
| 653 | mRecord.stopTrack(); |
| 654 | mPlayback.stopTrack(); |
Mikhail Naganov | d3f301c | 2019-03-11 08:58:03 -0700 | [diff] [blame] | 655 | mRecord.clearTrackPeer(); // mRecord stop is synchronous. Break PeerProxy sp<> cycle. |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 656 | mRecord.closeConnections(panel); |
| 657 | mPlayback.closeConnections(panel); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 660 | status_t AudioFlinger::PatchPanel::Patch::getLatencyMs(double *latencyMs) const |
| 661 | { |
| 662 | if (!isSoftware()) return INVALID_OPERATION; |
| 663 | |
| 664 | auto recordTrack = mRecord.const_track(); |
| 665 | if (recordTrack.get() == nullptr) return INVALID_OPERATION; |
| 666 | |
| 667 | auto playbackTrack = mPlayback.const_track(); |
| 668 | if (playbackTrack.get() == nullptr) return INVALID_OPERATION; |
| 669 | |
| 670 | // Latency information for tracks may be called without obtaining |
| 671 | // the underlying thread lock. |
| 672 | // |
| 673 | // We use record server latency + playback track latency (generally smaller than the |
| 674 | // reverse due to internal biases). |
| 675 | // |
| 676 | // TODO: is this stable enough? Consider a PatchTrack synchronized version of this. |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 677 | |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 678 | // For PCM tracks get server latency. |
| 679 | if (audio_is_linear_pcm(recordTrack->format())) { |
| 680 | double recordServerLatencyMs, playbackTrackLatencyMs; |
| 681 | if (recordTrack->getServerLatencyMs(&recordServerLatencyMs) == OK |
| 682 | && playbackTrack->getTrackLatencyMs(&playbackTrackLatencyMs) == OK) { |
| 683 | *latencyMs = recordServerLatencyMs + playbackTrackLatencyMs; |
| 684 | return OK; |
| 685 | } |
| 686 | } |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 687 | |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 688 | // See if kernel latencies are available. |
| 689 | // If so, do a frame diff and time difference computation to estimate |
| 690 | // the total patch latency. This requires that frame counts are reported by the |
| 691 | // HAL are matched properly in the case of record overruns and playback underruns. |
Andy Hung | d29af63 | 2023-06-23 19:27:19 -0700 | [diff] [blame^] | 692 | IAfTrack::FrameTime recordFT{}, playFT{}; |
Andy Hung | 3028256 | 2018-08-08 18:27:03 -0700 | [diff] [blame] | 693 | recordTrack->getKernelFrameTime(&recordFT); |
| 694 | playbackTrack->getKernelFrameTime(&playFT); |
| 695 | if (recordFT.timeNs > 0 && playFT.timeNs > 0) { |
| 696 | const int64_t frameDiff = recordFT.frames - playFT.frames; |
| 697 | const int64_t timeDiffNs = recordFT.timeNs - playFT.timeNs; |
| 698 | |
| 699 | // It is possible that the patch track and patch record have a large time disparity because |
| 700 | // one thread runs but another is stopped. We arbitrarily choose the maximum timestamp |
| 701 | // time difference based on how often we expect the timestamps to update in normal operation |
| 702 | // (typical should be no more than 50 ms). |
| 703 | // |
| 704 | // If the timestamps aren't sampled close enough, the patch latency is not |
| 705 | // considered valid. |
| 706 | // |
| 707 | // TODO: change this based on more experiments. |
| 708 | constexpr int64_t maxValidTimeDiffNs = 200 * NANOS_PER_MILLISECOND; |
| 709 | if (std::abs(timeDiffNs) < maxValidTimeDiffNs) { |
| 710 | *latencyMs = frameDiff * 1e3 / recordTrack->sampleRate() |
| 711 | - timeDiffNs * 1e-6; |
| 712 | return OK; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | return INVALID_OPERATION; |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 719 | String8 AudioFlinger::PatchPanel::Patch::dump(audio_patch_handle_t myHandle) const |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 720 | { |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 721 | // TODO: Consider table dump form for patches, just like tracks. |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 722 | String8 result = String8::format("Patch %d: %s (thread %p => thread %p)", |
| 723 | myHandle, isSoftware() ? "Software bridge between" : "No software bridge", |
| 724 | mRecord.const_thread().get(), mPlayback.const_thread().get()); |
| 725 | |
| 726 | bool hasSinkDevice = |
| 727 | mAudioPatch.num_sinks > 0 && mAudioPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE; |
| 728 | bool hasSourceDevice = |
| 729 | mAudioPatch.num_sources > 0 && mAudioPatch.sources[0].type == AUDIO_PORT_TYPE_DEVICE; |
| 730 | result.appendFormat(" thread %p %s (%d) first device type %08x", mThread.unsafe_get(), |
| 731 | hasSinkDevice ? "num sinks" : |
| 732 | (hasSourceDevice ? "num sources" : "no devices"), |
| 733 | hasSinkDevice ? mAudioPatch.num_sinks : |
| 734 | (hasSourceDevice ? mAudioPatch.num_sources : 0), |
| 735 | hasSinkDevice ? mAudioPatch.sinks[0].ext.device.type : |
| 736 | (hasSourceDevice ? mAudioPatch.sources[0].ext.device.type : 0)); |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 737 | |
| 738 | // add latency if it exists |
| 739 | double latencyMs; |
| 740 | if (getLatencyMs(&latencyMs) == OK) { |
Mikhail Naganov | b8b6097 | 2018-09-13 12:55:43 -0700 | [diff] [blame] | 741 | result.appendFormat(" latency: %.2lf ms", latencyMs); |
Andy Hung | c3ab773 | 2018-06-01 13:46:29 -0700 | [diff] [blame] | 742 | } |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 743 | return result; |
| 744 | } |
| 745 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 746 | /* Disconnect a patch */ |
| 747 | status_t AudioFlinger::PatchPanel::releaseAudioPatch(audio_patch_handle_t handle) |
Eric Laurent | 34e55a4 | 2023-04-24 16:37:56 +0200 | [diff] [blame] | 748 | //unlocks AudioFlinger::mLock when calling ThreadBase::sendReleaseAudioPatchConfigEvent |
| 749 | //to avoid deadlocks if the thread loop needs to acquire AudioFlinger::mLock |
| 750 | //before processing the release patch request. |
| 751 | NO_THREAD_SAFETY_ANALYSIS |
| 752 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 753 | ALOGV("%s handle %d", __func__, handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 754 | status_t status = NO_ERROR; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 755 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 756 | auto iter = mPatches.find(handle); |
| 757 | if (iter == mPatches.end()) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 758 | return BAD_VALUE; |
| 759 | } |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 760 | Patch &removedPatch = iter->second; |
| 761 | const struct audio_patch &patch = removedPatch.mAudioPatch; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 762 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 763 | const struct audio_port_config &src = patch.sources[0]; |
| 764 | switch (src.type) { |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 765 | case AUDIO_PORT_TYPE_DEVICE: { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 766 | sp<DeviceHalInterface> hwDevice = findHwDeviceByModule(src.ext.device.hw_module); |
| 767 | if (hwDevice == 0) { |
| 768 | ALOGW("%s() bad src hw module %d", __func__, src.ext.device.hw_module); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 769 | status = BAD_VALUE; |
| 770 | break; |
| 771 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 772 | |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 773 | if (removedPatch.isSoftware()) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 774 | removedPatch.clearConnections(this); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 775 | break; |
| 776 | } |
| 777 | |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 778 | if (patch.sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 779 | audio_io_handle_t ioHandle = patch.sinks[0].ext.mix.handle; |
| 780 | sp<ThreadBase> thread = mAudioFlinger.checkRecordThread_l(ioHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 781 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 782 | thread = mAudioFlinger.checkMmapThread_l(ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 783 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 784 | ALOGW("%s() bad capture I/O handle %d", __func__, ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 785 | status = BAD_VALUE; |
| 786 | break; |
| 787 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 788 | } |
Eric Laurent | 34e55a4 | 2023-04-24 16:37:56 +0200 | [diff] [blame] | 789 | mAudioFlinger.unlock(); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 790 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle); |
Eric Laurent | 34e55a4 | 2023-04-24 16:37:56 +0200 | [diff] [blame] | 791 | mAudioFlinger.lock(); |
Eric Laurent | 054d9d3 | 2015-04-24 08:48:48 -0700 | [diff] [blame] | 792 | } else { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 793 | status = hwDevice->releaseAudioPatch(removedPatch.mHalHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 794 | } |
| 795 | } break; |
| 796 | case AUDIO_PORT_TYPE_MIX: { |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 797 | if (findHwDeviceByModule(src.ext.mix.hw_module) == 0) { |
| 798 | ALOGW("%s() bad src hw module %d", __func__, src.ext.mix.hw_module); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 799 | status = BAD_VALUE; |
| 800 | break; |
| 801 | } |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 802 | audio_io_handle_t ioHandle = src.ext.mix.handle; |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 803 | sp<ThreadBase> thread = mAudioFlinger.checkPlaybackThread_l(ioHandle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 804 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 805 | thread = mAudioFlinger.checkMmapThread_l(ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 806 | if (thread == 0) { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 807 | ALOGW("%s() bad playback I/O handle %d", __func__, ioHandle); |
Eric Laurent | 6acd1d4 | 2017-01-04 14:23:29 -0800 | [diff] [blame] | 808 | status = BAD_VALUE; |
| 809 | break; |
| 810 | } |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 811 | } |
Eric Laurent | 34e55a4 | 2023-04-24 16:37:56 +0200 | [diff] [blame] | 812 | mAudioFlinger.unlock(); |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 813 | status = thread->sendReleaseAudioPatchConfigEvent(removedPatch.mHalHandle); |
Eric Laurent | 34e55a4 | 2023-04-24 16:37:56 +0200 | [diff] [blame] | 814 | mAudioFlinger.lock(); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 815 | } break; |
| 816 | default: |
| 817 | status = BAD_VALUE; |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 820 | erasePatch(handle); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 821 | return status; |
| 822 | } |
| 823 | |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 824 | void AudioFlinger::PatchPanel::erasePatch(audio_patch_handle_t handle, bool reuseExistingHalPatch) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 825 | mPatches.erase(handle); |
| 826 | removeSoftwarePatchFromInsertedModules(handle); |
François Gaffie | 58e73af | 2023-02-15 11:47:24 +0100 | [diff] [blame] | 827 | if (!reuseExistingHalPatch) { |
| 828 | mAudioFlinger.mPatchCommandThread->releaseAudioPatch(handle); |
| 829 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 830 | } |
| 831 | |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 832 | /* List connected audio ports and they attributes */ |
| 833 | status_t AudioFlinger::PatchPanel::listAudioPatches(unsigned int *num_patches __unused, |
| 834 | struct audio_patch *patches __unused) |
| 835 | { |
Mikhail Naganov | dea5304 | 2018-04-26 13:10:21 -0700 | [diff] [blame] | 836 | ALOGV(__func__); |
Eric Laurent | 951f455 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 837 | return NO_ERROR; |
| 838 | } |
| 839 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 840 | status_t AudioFlinger::PatchPanel::getDownstreamSoftwarePatches( |
| 841 | audio_io_handle_t stream, |
| 842 | std::vector<AudioFlinger::PatchPanel::SoftwarePatch> *patches) const |
| 843 | { |
| 844 | for (const auto& module : mInsertedModules) { |
| 845 | if (module.second.streams.count(stream)) { |
| 846 | for (const auto& patchHandle : module.second.sw_patches) { |
| 847 | const auto& patch_iter = mPatches.find(patchHandle); |
| 848 | if (patch_iter != mPatches.end()) { |
| 849 | const Patch &patch = patch_iter->second; |
| 850 | patches->emplace_back(*this, patchHandle, |
| 851 | patch.mPlayback.const_thread()->id(), |
| 852 | patch.mRecord.const_thread()->id()); |
| 853 | } else { |
| 854 | ALOGE("Stale patch handle in the cache: %d", patchHandle); |
| 855 | } |
| 856 | } |
| 857 | return OK; |
| 858 | } |
| 859 | } |
| 860 | // The stream is not associated with any of inserted modules. |
| 861 | return BAD_VALUE; |
| 862 | } |
| 863 | |
| 864 | void AudioFlinger::PatchPanel::notifyStreamOpened( |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 865 | AudioHwDevice *audioHwDevice, audio_io_handle_t stream, struct audio_patch *patch) |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 866 | { |
| 867 | if (audioHwDevice->isInsert()) { |
| 868 | mInsertedModules[audioHwDevice->handle()].streams.insert(stream); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 869 | if (patch != nullptr) { |
| 870 | std::vector <SoftwarePatch> swPatches; |
| 871 | getDownstreamSoftwarePatches(stream, &swPatches); |
| 872 | if (swPatches.size() > 0) { |
| 873 | auto iter = mPatches.find(swPatches[0].getPatchHandle()); |
| 874 | if (iter != mPatches.end()) { |
| 875 | *patch = iter->second.mAudioPatch; |
| 876 | } |
| 877 | } |
| 878 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
| 882 | void AudioFlinger::PatchPanel::notifyStreamClosed(audio_io_handle_t stream) |
| 883 | { |
| 884 | for (auto& module : mInsertedModules) { |
| 885 | module.second.streams.erase(stream); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | AudioHwDevice* AudioFlinger::PatchPanel::findAudioHwDeviceByModule(audio_module_handle_t module) |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 890 | { |
| 891 | if (module == AUDIO_MODULE_HANDLE_NONE) return nullptr; |
| 892 | ssize_t index = mAudioFlinger.mAudioHwDevs.indexOfKey(module); |
| 893 | if (index < 0) { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 894 | ALOGW("%s() bad hw module %d", __func__, module); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 895 | return nullptr; |
| 896 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 897 | return mAudioFlinger.mAudioHwDevs.valueAt(index); |
Mikhail Naganov | 444ecc3 | 2018-05-01 17:40:05 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 900 | sp<DeviceHalInterface> AudioFlinger::PatchPanel::findHwDeviceByModule(audio_module_handle_t module) |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 901 | { |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 902 | AudioHwDevice *audioHwDevice = findAudioHwDeviceByModule(module); |
| 903 | return audioHwDevice ? audioHwDevice->hwDevice() : nullptr; |
| 904 | } |
| 905 | |
| 906 | void AudioFlinger::PatchPanel::addSoftwarePatchToInsertedModules( |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 907 | audio_module_handle_t module, audio_patch_handle_t handle, |
| 908 | const struct audio_patch *patch) |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 909 | { |
| 910 | mInsertedModules[module].sw_patches.insert(handle); |
Eric Laurent | 74c38dc | 2020-12-23 18:19:44 +0100 | [diff] [blame] | 911 | if (!mInsertedModules[module].streams.empty()) { |
| 912 | mAudioFlinger.updateDownStreamPatches_l(patch, mInsertedModules[module].streams); |
| 913 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | void AudioFlinger::PatchPanel::removeSoftwarePatchFromInsertedModules( |
| 917 | audio_patch_handle_t handle) |
| 918 | { |
| 919 | for (auto& module : mInsertedModules) { |
| 920 | module.second.sw_patches.erase(handle); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | void AudioFlinger::PatchPanel::dump(int fd) const |
| 925 | { |
| 926 | String8 patchPanelDump; |
| 927 | const char *indent = " "; |
| 928 | |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 929 | bool headerPrinted = false; |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 930 | for (const auto& iter : mPatches) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 931 | if (!headerPrinted) { |
| 932 | patchPanelDump += "\nPatches:\n"; |
| 933 | headerPrinted = true; |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 934 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 935 | patchPanelDump.appendFormat("%s%s\n", indent, iter.second.dump(iter.first).string()); |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 936 | } |
Mikhail Naganov | adca70f | 2018-07-09 12:49:25 -0700 | [diff] [blame] | 937 | |
| 938 | headerPrinted = false; |
| 939 | for (const auto& module : mInsertedModules) { |
| 940 | if (!module.second.streams.empty() || !module.second.sw_patches.empty()) { |
| 941 | if (!headerPrinted) { |
| 942 | patchPanelDump += "\nTracked inserted modules:\n"; |
| 943 | headerPrinted = true; |
| 944 | } |
| 945 | String8 moduleDump = String8::format("Module %d: I/O handles: ", module.first); |
| 946 | for (const auto& stream : module.second.streams) { |
| 947 | moduleDump.appendFormat("%d ", stream); |
| 948 | } |
| 949 | moduleDump.append("; SW Patches: "); |
| 950 | for (const auto& patch : module.second.sw_patches) { |
| 951 | moduleDump.appendFormat("%d ", patch); |
| 952 | } |
| 953 | patchPanelDump.appendFormat("%s%s\n", indent, moduleDump.string()); |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | if (!patchPanelDump.isEmpty()) { |
| 958 | write(fd, patchPanelDump.string(), patchPanelDump.size()); |
Mikhail Naganov | 201369b | 2018-05-16 16:52:32 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
Glenn Kasten | 63238ef | 2015-03-02 15:50:29 -0800 | [diff] [blame] | 962 | } // namespace android |