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