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